From 1753d2bf6ec305250a168bb50b651e8d76ae7b93 Mon Sep 17 00:00:00 2001 From: bunsenstraat Date: Fri, 13 May 2022 10:09:10 +0200 Subject: [PATCH] references --- apps/remix-ide/src/app/editor/editor.js | 2 +- .../src/lib/editor-context-listener.ts | 56 +++++++++++++++++-- .../src/lib/remix-ui-editor-context-view.tsx | 7 ++- .../remix-ui/editor/src/lib/actions/editor.ts | 1 + .../editor/src/lib/remix-ui-editor.tsx | 32 ++++++++++- 5 files changed, 85 insertions(+), 13 deletions(-) diff --git a/apps/remix-ide/src/app/editor/editor.js b/apps/remix-ide/src/app/editor/editor.js index 9fe194eeab..ffd0b64652 100644 --- a/apps/remix-ide/src/app/editor/editor.js +++ b/apps/remix-ide/src/app/editor/editor.js @@ -12,7 +12,7 @@ const profile = { name: 'editor', description: 'service - editor', version: packageJson.version, - methods: ['highlight', 'discardHighlight', 'clearAnnotations', 'addAnnotation', 'gotoLine', 'revealRange', 'getCursorPosition'] + methods: ['highlight', 'discardHighlight', 'clearAnnotations', 'addAnnotation', 'gotoLine', 'revealRange', 'getCursorPosition', 'open'] } class Editor extends Plugin { diff --git a/libs/remix-core-plugin/src/lib/editor-context-listener.ts b/libs/remix-core-plugin/src/lib/editor-context-listener.ts index 43a555f588..e2445e28d9 100644 --- a/libs/remix-core-plugin/src/lib/editor-context-listener.ts +++ b/libs/remix-core-plugin/src/lib/editor-context-listener.ts @@ -6,7 +6,7 @@ import { AstNode } from '@remix-project/remix-solidity-ts' const profile = { name: 'contextualListener', - methods: ['jumpToDefinition', 'nodesAtEditorPosition', 'referencesOf', 'getActiveHighlights', 'gasEstimation', 'declarationOf', 'jumpTo'], + methods: ['jumpToDefinition', 'referrencesAtPosition', 'nodesAtEditorPosition', 'referencesOf', 'getActiveHighlights', 'gasEstimation', 'declarationOf', 'jumpToPosition'], events: [], version: '0.0.1' } @@ -60,7 +60,6 @@ export class EditorContextListener extends Plugin { FlatReferences: {} } this._buildIndex(data, source) - console.log(this._index) }) setInterval(async () => { @@ -90,15 +89,36 @@ export class EditorContextListener extends Plugin { declarationOf(node) { if (node && node.referencedDeclaration) { return this._index.FlatReferences[node.referencedDeclaration] + } else { + // console.log(this._index.FlatReferences) } return null } 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') if (lastCompilationResult && lastCompilationResult.languageversion.indexOf('soljson') === 0 && lastCompilationResult.data) { const nodes = sourceMappingDecoder.nodesAtPosition(null, position, lastCompilationResult.data.sources[this.currentFile]) @@ -107,6 +127,16 @@ export class EditorContextListener extends Plugin { 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) { const nodes = await this.nodesAtEditorPosition(position) console.log(nodes) @@ -120,14 +150,27 @@ export class EditorContextListener extends Plugin { nodeDeclaration = node } } - console.log(node, nodeDeclaration) + // console.log(node, nodeDeclaration) if (nodeDeclaration && nodeDeclaration.src) { - console.log(nodeDeclaration) + //console.log(nodeDeclaration) const position = sourceMappingDecoder.decode(nodeDeclaration.src) if (position) { + console.log('jump to', 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 @@ -243,6 +286,7 @@ export class EditorContextListener extends Plugin { await highlights(node.id) await this._highlight(node, compilationResult) } + this.results = compilationResult } diff --git a/libs/remix-ui/editor-context-view/src/lib/remix-ui-editor-context-view.tsx b/libs/remix-ui/editor-context-view/src/lib/remix-ui-editor-context-view.tsx index f67ef72c56..962c43e09a 100644 --- a/libs/remix-ui/editor-context-view/src/lib/remix-ui-editor-context-view.tsx +++ b/libs/remix-ui/editor-context-view/src/lib/remix-ui-editor-context-view.tsx @@ -86,7 +86,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps) nextNodeDeclaration = nextNode } } - console.log(nextNode, nextNodeDeclaration) + //console.log(nextNode, nextNodeDeclaration) if (nextNodeDeclaration && currentNodeDeclaration.current && nextNodeDeclaration.id === currentNodeDeclaration.current.id) return currentNodeDeclaration.current = nextNodeDeclaration @@ -98,6 +98,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps) } } const activeHighlights: Array = await props.getActiveHighlights() + console.log('active highlights', activeHighlights) if (nextNode && activeHighlights && activeHighlights.length) { 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 @@ -136,7 +137,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps) const node = currentNodeDeclaration.current if (!node) return (
) const references = state.activeHighlights - console.log(node) + // console.log(node) const type = node.typeDescriptions && node.typeDescriptions.typeString ? node.typeDescriptions.typeString : node.nodeType const referencesCount = `${references ? references.length : '0'} reference(s)` @@ -144,7 +145,7 @@ export function RemixUiEditorContextView (props: RemixUiEditorContextViewProps) const jumpTo = () => { if (node && node.src) { - console.log(node) + // console.log(node) const position = sourceMappingDecoder.decode(node.src) if (position) { props.jumpToPosition(position) diff --git a/libs/remix-ui/editor/src/lib/actions/editor.ts b/libs/remix-ui/editor/src/lib/actions/editor.ts index b718ffca5a..0c968e1f07 100644 --- a/libs/remix-ui/editor/src/lib/actions/editor.ts +++ b/libs/remix-ui/editor/src/lib/actions/editor.ts @@ -21,6 +21,7 @@ export const reducerActions = (models = initialState, action: Action) => { const readOnly = action.payload.readOnly if (models[uri]) return models // already existing models[uri] = { language, uri, readOnly } + console.log(uri) const model = monaco.editor.createModel(value, language, monaco.Uri.parse(uri)) models[uri].model = model model.onDidChangeContent(() => action.payload.events.onDidChangeContent(uri)) diff --git a/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx b/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx index 306595d0be..23e87a3259 100644 --- a/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx +++ b/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx @@ -430,10 +430,36 @@ export const EditorUI = (props: EditorUIProps) => { provideDefinition(model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken){ const cursorPosition = props.editorAPI.getCursorPosition() 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', { provideHover: async function (model: any, position: monaco.Position) { @@ -458,11 +484,11 @@ export const EditorUI = (props: EditorUIProps) => { range: new monaco.Range( position.lineNumber, position.column, - position.lineNumber, + position.lineNumber, model.getLineMaxColumn(position.lineNumber) ), contents: [ - { value: '
test html
' } + ] }; }