pull/5370/head
bunsenstraat 3 years ago
parent eb4e2b42bb
commit 71aea1e53b
  1. 2
      apps/remix-ide/src/app/editor/editor.js
  2. 56
      libs/remix-core-plugin/src/lib/editor-context-listener.ts
  3. 7
      libs/remix-ui/editor-context-view/src/lib/remix-ui-editor-context-view.tsx
  4. 1
      libs/remix-ui/editor/src/lib/actions/editor.ts
  5. 32
      libs/remix-ui/editor/src/lib/remix-ui-editor.tsx

@ -12,7 +12,7 @@ const profile = {
name: 'editor', name: 'editor',
description: 'service - editor', description: 'service - editor',
version: packageJson.version, version: packageJson.version,
methods: ['highlight', 'discardHighlight', 'clearAnnotations', 'addAnnotation', 'gotoLine', 'revealRange', 'getCursorPosition'] methods: ['highlight', 'discardHighlight', 'clearAnnotations', 'addAnnotation', 'gotoLine', 'revealRange', 'getCursorPosition', 'open']
} }
class Editor extends Plugin { class Editor extends Plugin {

@ -6,7 +6,7 @@ import { AstNode } from '@remix-project/remix-solidity-ts'
const profile = { const profile = {
name: 'contextualListener', name: 'contextualListener',
methods: ['jumpToDefinition', 'nodesAtEditorPosition', 'referencesOf', 'getActiveHighlights', 'gasEstimation', 'declarationOf', 'jumpTo'], methods: ['jumpToDefinition', 'referrencesAtPosition', 'nodesAtEditorPosition', 'referencesOf', 'getActiveHighlights', 'gasEstimation', 'declarationOf', 'jumpToPosition'],
events: [], events: [],
version: '0.0.1' version: '0.0.1'
} }
@ -60,7 +60,6 @@ export class EditorContextListener extends Plugin {
FlatReferences: {} FlatReferences: {}
} }
this._buildIndex(data, source) this._buildIndex(data, source)
console.log(this._index)
}) })
setInterval(async () => { setInterval(async () => {
@ -90,15 +89,36 @@ export class EditorContextListener extends Plugin {
declarationOf(node) { declarationOf(node) {
if (node && node.referencedDeclaration) { if (node && node.referencedDeclaration) {
return this._index.FlatReferences[node.referencedDeclaration] return this._index.FlatReferences[node.referencedDeclaration]
} else {
// console.log(this._index.FlatReferences)
} }
return null return null
} }
referencesOf(node) { referencesOf(node) {
return this._index.Declarations[node.id] const results = []
const highlights = (id) => {
if (this._index.Declarations && this._index.Declarations[id]) {
const refs = this._index.Declarations[id]
for (const ref in refs) {
const node = refs[ref]
//console.log('reference', node)
results.push(node)
}
}
}
if (node && node.referencedDeclaration) {
highlights(node.referencedDeclaration)
const current = this._index.FlatReferences[node.referencedDeclaration]
results.push(current)
} else {
highlights(node.id)
}
//console.log(results)
} }
async nodesAtEditorPosition(position: any){ async nodesAtEditorPosition(position: any) {
const lastCompilationResult = await this.call('compilerArtefacts', 'getLastCompilationResult') const lastCompilationResult = await this.call('compilerArtefacts', 'getLastCompilationResult')
if (lastCompilationResult && lastCompilationResult.languageversion.indexOf('soljson') === 0 && lastCompilationResult.data) { if (lastCompilationResult && lastCompilationResult.languageversion.indexOf('soljson') === 0 && lastCompilationResult.data) {
const nodes = sourceMappingDecoder.nodesAtPosition(null, position, lastCompilationResult.data.sources[this.currentFile]) const nodes = sourceMappingDecoder.nodesAtPosition(null, position, lastCompilationResult.data.sources[this.currentFile])
@ -107,6 +127,16 @@ export class EditorContextListener extends Plugin {
return [] return []
} }
async referrencesAtPosition(position: any) {
const nodes = await this.nodesAtEditorPosition(position)
if (nodes && nodes.length) {
const node = nodes[nodes.length - 1]
if (node) {
return this.referencesOf(node)
}
}
}
async jumpToDefinition(position: any) { async jumpToDefinition(position: any) {
const nodes = await this.nodesAtEditorPosition(position) const nodes = await this.nodesAtEditorPosition(position)
console.log(nodes) console.log(nodes)
@ -120,14 +150,27 @@ export class EditorContextListener extends Plugin {
nodeDeclaration = node nodeDeclaration = node
} }
} }
console.log(node, nodeDeclaration) // console.log(node, nodeDeclaration)
if (nodeDeclaration && nodeDeclaration.src) { if (nodeDeclaration && nodeDeclaration.src) {
console.log(nodeDeclaration) //console.log(nodeDeclaration)
const position = sourceMappingDecoder.decode(nodeDeclaration.src) const position = sourceMappingDecoder.decode(nodeDeclaration.src)
if (position) { if (position) {
console.log('jump to', position)
await this.jumpToPosition(position) await this.jumpToPosition(position)
} }
} }
// jump to import
if (node && node.nodeType === 'ImportDirective') {
console.log(this._index.FlatReferences)
// loop over this._index.FlatReferences
for (const key in this._index.FlatReferences) {
if (this._index.FlatReferences[key].id === node.sourceUnit) {
console.log('jump to', this._index.FlatReferences[key])
const position = sourceMappingDecoder.decode(this._index.FlatReferences[key].src)
this.jumpToPosition(position)
}
}
}
} }
/* /*
* onClick jump to position of ast node in the editor * onClick jump to position of ast node in the editor
@ -243,6 +286,7 @@ export class EditorContextListener extends Plugin {
await highlights(node.id) await highlights(node.id)
await this._highlight(node, compilationResult) await this._highlight(node, compilationResult)
} }
this.results = compilationResult this.results = compilationResult
} }

@ -86,7 +86,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps)
nextNodeDeclaration = nextNode nextNodeDeclaration = nextNode
} }
} }
console.log(nextNode, nextNodeDeclaration) //console.log(nextNode, nextNodeDeclaration)
if (nextNodeDeclaration && currentNodeDeclaration.current && nextNodeDeclaration.id === currentNodeDeclaration.current.id) return if (nextNodeDeclaration && currentNodeDeclaration.current && nextNodeDeclaration.id === currentNodeDeclaration.current.id) return
currentNodeDeclaration.current = nextNodeDeclaration currentNodeDeclaration.current = nextNodeDeclaration
@ -98,6 +98,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps)
} }
} }
const activeHighlights: Array<astNodeLight> = await props.getActiveHighlights() const activeHighlights: Array<astNodeLight> = await props.getActiveHighlights()
console.log('active highlights', activeHighlights)
if (nextNode && activeHighlights && activeHighlights.length) { if (nextNode && activeHighlights && activeHighlights.length) {
loopOverReferences.current = activeHighlights.findIndex((el: astNodeLight) => `${el.position.start}:${el.position.length}:${el.position.file}` === nextNode.src) loopOverReferences.current = activeHighlights.findIndex((el: astNodeLight) => `${el.position.start}:${el.position.length}:${el.position.file}` === nextNode.src)
loopOverReferences.current = loopOverReferences.current === -1 ? 0 : loopOverReferences.current loopOverReferences.current = loopOverReferences.current === -1 ? 0 : loopOverReferences.current
@ -136,7 +137,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps)
const node = currentNodeDeclaration.current const node = currentNodeDeclaration.current
if (!node) return (<div></div>) if (!node) return (<div></div>)
const references = state.activeHighlights const references = state.activeHighlights
console.log(node) // console.log(node)
const type = node.typeDescriptions && node.typeDescriptions.typeString ? node.typeDescriptions.typeString : node.nodeType const type = node.typeDescriptions && node.typeDescriptions.typeString ? node.typeDescriptions.typeString : node.nodeType
const referencesCount = `${references ? references.length : '0'} reference(s)` const referencesCount = `${references ? references.length : '0'} reference(s)`
@ -144,7 +145,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps)
const jumpTo = () => { const jumpTo = () => {
if (node && node.src) { if (node && node.src) {
console.log(node) // console.log(node)
const position = sourceMappingDecoder.decode(node.src) const position = sourceMappingDecoder.decode(node.src)
if (position) { if (position) {
props.jumpToPosition(position) props.jumpToPosition(position)

@ -21,6 +21,7 @@ export const reducerActions = (models = initialState, action: Action) => {
const readOnly = action.payload.readOnly const readOnly = action.payload.readOnly
if (models[uri]) return models // already existing if (models[uri]) return models // already existing
models[uri] = { language, uri, readOnly } models[uri] = { language, uri, readOnly }
console.log(uri)
const model = monaco.editor.createModel(value, language, monaco.Uri.parse(uri)) const model = monaco.editor.createModel(value, language, monaco.Uri.parse(uri))
models[uri].model = model models[uri].model = model
model.onDidChangeContent(() => action.payload.events.onDidChangeContent(uri)) model.onDidChangeContent(() => action.payload.events.onDidChangeContent(uri))

@ -430,10 +430,36 @@ export const EditorUI = (props: EditorUIProps) => {
provideDefinition(model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken){ provideDefinition(model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken){
const cursorPosition = props.editorAPI.getCursorPosition() const cursorPosition = props.editorAPI.getCursorPosition()
props.plugin.call('contextualListener', 'jumpToDefinition', cursorPosition) props.plugin.call('contextualListener', 'jumpToDefinition', cursorPosition)
return null
} }
}) })
monacoRef.current.languages.registerReferenceProvider('remix-solidity', {
provideReferences(model: monaco.editor.ITextModel, position: monaco.Position, context: any, token: monaco.CancellationToken){
const cursorPosition = props.editorAPI.getCursorPosition()
const references = props.plugin.call('contextualListener', 'referrencesAtPosition', cursorPosition)
console.log(references)
return [{
range: new monaco.Range(
0,
0,
0,
4
),
uri: monaco.Uri.parse('test.sol'),
},
{
range: new monaco.Range(
0,
0,
0,
4
),
uri: monaco.Uri.parse('test2.sol'),
}]
}
})
monacoRef.current.languages.registerHoverProvider('remix-solidity', { monacoRef.current.languages.registerHoverProvider('remix-solidity', {
provideHover: async function (model: any, position: monaco.Position) { provideHover: async function (model: any, position: monaco.Position) {
@ -458,11 +484,11 @@ export const EditorUI = (props: EditorUIProps) => {
range: new monaco.Range( range: new monaco.Range(
position.lineNumber, position.lineNumber,
position.column, position.column,
position.lineNumber, position.lineNumber,
model.getLineMaxColumn(position.lineNumber) model.getLineMaxColumn(position.lineNumber)
), ),
contents: [ contents: [
{ value: '<div>test html</div>' }
] ]
}; };
} }

Loading…
Cancel
Save