Merge pull request #2850 from ethereum/fixdefinitionprovider

fix definition provider
pull/2856/head
bunsenstraat 2 years ago committed by GitHub
commit a458f0034b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      libs/remix-ui/editor/src/lib/providers/definitionProvider.ts
  2. 8
      libs/remix-ui/editor/src/lib/remix-ui-editor.tsx

@ -13,15 +13,24 @@ export class RemixDefinitionProvider implements monaco.languages.DefinitionProvi
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async provideDefinition(model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken): Promise<monaco.languages.Definition | monaco.languages.LocationLink[]> {
const cursorPosition = this.props.editorAPI.getCursorPosition()
await this.jumpToDefinition(cursorPosition)
return null
const jumpLocation = await this.jumpToDefinition(cursorPosition)
return [{
uri: this.monaco.Uri.parse(jumpLocation.fileName),
range: {
startLineNumber: jumpLocation.startLineNumber,
startColumn: jumpLocation.startColumn,
endLineNumber: jumpLocation.endLineNumber,
endColumn: jumpLocation.endColumn
}
}]
}
async jumpToDefinition(position: any) {
const node = await this.props.plugin.call('codeParser', 'definitionAtPosition', position)
const sourcePosition = await this.props.plugin.call('codeParser', 'positionOfDefinition', node)
if (sourcePosition) {
await this.jumpToPosition(sourcePosition)
return await this.jumpToPosition(sourcePosition)
}
}
@ -30,12 +39,25 @@ export class RemixDefinitionProvider implements monaco.languages.DefinitionProvi
*/
async jumpToPosition(position: any) {
const jumpToLine = async (fileName: string, lineColumn: any) => {
const fileTarget = await this.props.plugin.call('fileManager', 'getPathFromUrl', fileName)
if (fileName !== await this.props.plugin.call('fileManager', 'file')) {
await this.props.plugin.call('contentImport', 'resolveAndSave', fileName, null)
await this.props.plugin.call('fileManager', 'open', fileName)
const fileContent = await this.props.plugin.call('fileManager', 'readFile', fileName)
try {
await this.props.plugin.call('editor', 'addModel', fileTarget.file, fileContent)
} catch (e) {
}
}
if (lineColumn.start && lineColumn.start.line >= 0 && lineColumn.start.column >= 0) {
this.props.plugin.call('editor', 'gotoLine', lineColumn.start.line, lineColumn.end.column + 1)
const pos = {
startLineNumber: lineColumn.start.line + 1,
startColumn: lineColumn.start.column + 1,
endColumn: lineColumn.end.column + 1,
endLineNumber: lineColumn.end.line + 1,
fileName: (fileTarget && fileTarget.file) || fileName
}
return pos
}
}
const lastCompilationResult = await this.props.plugin.call('codeParser', 'getLastCompilationResult') // await this.props.plugin.call('compilerArtefacts', 'getLastCompilationResult')
@ -43,7 +65,7 @@ export class RemixDefinitionProvider implements monaco.languages.DefinitionProvi
const lineColumn = await this.props.plugin.call('codeParser', 'getLineColumnOfPosition', position)
const filename = lastCompilationResult.getSourceName(position.file)
jumpToLine(filename, lineColumn)
return await jumpToLine(filename, lineColumn)
}
}
}

@ -569,14 +569,17 @@ export const EditorUI = (props: EditorUIProps) => {
}
editor.addAction(zoomOutAction)
editor.addAction(zoominAction)
const editorService = editor._codeEditorService;
const openEditorBase = editorService.openCodeEditor.bind(editorService);
editorService.openCodeEditor = async (input, source) => {
editorService.openCodeEditor = async (input , source) => {
const result = await openEditorBase(input, source)
if (input && input.resource && input.resource.path) {
try {
await props.plugin.call('fileManager', 'open', input.resource.path)
if (input.options && input.options.selection) {
editor.revealRange(input.options.selection)
editor.setPosition({ column: input.options.selection.startColumn, lineNumber: input.options.selection.startLineNumber })
}
} catch (e) {
console.log(e)
}
@ -608,6 +611,7 @@ export const EditorUI = (props: EditorUIProps) => {
monacoRef.current.languages.registerHoverProvider('remix-solidity', new RemixHoverProvider(props, monaco))
monacoRef.current.languages.registerCompletionItemProvider('remix-solidity', new RemixCompletionProvider(props, monaco))
loadTypes(monacoRef.current)
}

Loading…
Cancel
Save