editorcontextDummy
bunsenstraat 3 years ago
parent 9f26cf1ff6
commit 360ce32f70
  1. 7
      apps/remix-ide/src/app/editor/editor.js
  2. 56
      libs/remix-core-plugin/src/lib/editor-context-listener.ts
  3. 9
      libs/remix-ui/editor/src/lib/actions/editor.ts
  4. 105
      libs/remix-ui/editor/src/lib/remix-ui-editor.tsx
  5. 2
      libs/remix-ui/run-tab/src/lib/components/environment.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', 'open'] methods: ['highlight', 'discardHighlight', 'clearAnnotations', 'addAnnotation', 'gotoLine', 'revealRange', 'getCursorPosition', 'open', 'addModel']
} }
class Editor extends Plugin { class Editor extends Plugin {
@ -163,6 +163,7 @@ class Editor extends Plugin {
} }
async _onChange (file) { async _onChange (file) {
console.log(file)
const currentFile = await this.call('fileManager', 'file') const currentFile = await this.call('fileManager', 'file')
if (!currentFile) { if (!currentFile) {
return return
@ -244,6 +245,10 @@ class Editor extends Plugin {
return this.api.findMatches(this.currentFile, string) return this.api.findMatches(this.currentFile, string)
} }
addModel(path, content) {
this.emit('addModel', content, this._getMode(path), path, false)
}
/** /**
* Display an Empty read-only session * Display an Empty read-only session
*/ */

@ -6,7 +6,7 @@ import { AstNode } from '@remix-project/remix-solidity-ts'
const profile = { const profile = {
name: 'contextualListener', name: 'contextualListener',
methods: ['jumpToDefinition', 'referrencesAtPosition', 'nodesAtEditorPosition', 'referencesOf', 'getActiveHighlights', 'gasEstimation', 'declarationOf', 'jumpToPosition'], methods: ['definitionAtPosition', 'jumpToDefinition', 'referrencesAtPosition', 'nodesAtEditorPosition', 'referencesOf', 'getActiveHighlights', 'gasEstimation', 'declarationOf', 'jumpToPosition'],
events: [], events: [],
version: '0.0.1' version: '0.0.1'
} }
@ -95,14 +95,13 @@ export class EditorContextListener extends Plugin {
return null return null
} }
referencesOf(node) { referencesOf(node: AstNode) {
const results = [] const results = []
const highlights = (id) => { const highlights = (id) => {
if (this._index.Declarations && this._index.Declarations[id]) { if (this._index.Declarations && this._index.Declarations[id]) {
const refs = this._index.Declarations[id] const refs = this._index.Declarations[id]
for (const ref in refs) { for (const ref in refs) {
const node = refs[ref] const node = refs[ref]
//console.log('reference', node)
results.push(node) results.push(node)
} }
} }
@ -114,8 +113,7 @@ export class EditorContextListener extends Plugin {
} else { } else {
highlights(node.id) highlights(node.id)
} }
return results
//console.log(results)
} }
async nodesAtEditorPosition(position: any) { async nodesAtEditorPosition(position: any) {
@ -137,37 +135,43 @@ export class EditorContextListener extends Plugin {
} }
} }
async jumpToDefinition(position: any) { async getNodeDefinition() {
}
async definitionAtPosition(position: any) {
const nodes = await this.nodesAtEditorPosition(position) const nodes = await this.nodesAtEditorPosition(position)
console.log(nodes) console.log(nodes)
let nodeDeclaration: AstNode console.log(this._index.FlatReferences)
let nodeDefinition: AstNode
let node: AstNode let node: AstNode
if (nodes && nodes.length) { if (nodes && nodes.length) {
node = nodes[nodes.length - 1] node = nodes[nodes.length - 1]
nodeDefinition = node
if (!isDefinition(node)) { if (!isDefinition(node)) {
nodeDeclaration = await this.declarationOf(node) nodeDefinition = await this.declarationOf(node) || node
} else {
nodeDeclaration = node
} }
if (node.nodeType === 'ImportDirective') {
for (const key in this._index.FlatReferences) {
if (this._index.FlatReferences[key].id === node.sourceUnit) {
nodeDefinition = this._index.FlatReferences[key]
} }
// console.log(node, nodeDeclaration)
if (nodeDeclaration && nodeDeclaration.src) {
//console.log(nodeDeclaration)
const position = sourceMappingDecoder.decode(nodeDeclaration.src)
if (position) {
console.log('jump to', position)
await this.jumpToPosition(position)
} }
} }
// jump to import return nodeDefinition
if (node && node.nodeType === 'ImportDirective') { }else{
console.log(this._index.FlatReferences) console.log('no node found')
// 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) async jumpToDefinition(position: any) {
this.jumpToPosition(position) const node = await this.definitionAtPosition(position)
if (node) {
if (node.src) {
const position = sourceMappingDecoder.decode(node.src)
if (position) {
await this.jumpToPosition(position)
} }
} }
} }

@ -21,9 +21,14 @@ 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) let model
const model = monaco.editor.createModel(value, language, monaco.Uri.parse(uri)) try{
model = monaco.editor.createModel(value, language, monaco.Uri.parse(uri))
}catch(e){
}
models[uri].model = model models[uri].model = model
console.log('ADD_MODEL', models[uri].model)
model.onDidChangeContent(() => action.payload.events.onDidChangeContent(uri)) model.onDidChangeContent(() => action.payload.events.onDidChangeContent(uri))
return models return models
} }

@ -358,7 +358,7 @@ export const EditorUI = (props: EditorUIProps) => {
const model = editorModelsState[currentFileRef.current]?.model const model = editorModelsState[currentFileRef.current]?.model
if (model) { if (model) {
return model.getOffsetAt(position) return model.getOffsetAt(position)
}else{ } else {
return 0 return 0
} }
} }
@ -427,35 +427,47 @@ export const EditorUI = (props: EditorUIProps) => {
// register Definition Provider // register Definition Provider
monacoRef.current.languages.registerDefinitionProvider('remix-solidity', { monacoRef.current.languages.registerDefinitionProvider('remix-solidity', {
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)
} }
}) })
monacoRef.current.languages.registerReferenceProvider('remix-solidity', { monacoRef.current.languages.registerReferenceProvider('remix-solidity', {
provideReferences(model: monaco.editor.ITextModel, position: monaco.Position, context: any, token: monaco.CancellationToken){ async provideReferences(model: monaco.editor.ITextModel, position: monaco.Position, context: any, token: monaco.CancellationToken) {
const cursorPosition = props.editorAPI.getCursorPosition() const cursorPosition = props.editorAPI.getCursorPosition()
const references = props.plugin.call('contextualListener', 'referrencesAtPosition', cursorPosition) const nodes = await props.plugin.call('contextualListener', 'referrencesAtPosition', cursorPosition)
console.log(references) const references = []
return [{ if (nodes && nodes.length) {
range: new monaco.Range( const compilationResult = await props.plugin.call('compilerArtefacts', 'getLastCompilationResult')
0, const file = await props.plugin.call('fileManager', 'file')
0, if (compilationResult && compilationResult.data && compilationResult.data.sources[file]) {
0, for (const node of nodes) {
4 const position = sourceMappingDecoder.decode(node.src)
), const fileInNode = compilationResult.getSourceName(position.file)
uri: monaco.Uri.parse('test.sol'), let fileTarget = await props.plugin.call('fileManager', 'getPathFromUrl', fileInNode)
}, fileTarget = fileTarget.file
{ const fileContent = await props.plugin.call('fileManager', 'readFile', fileInNode)
range: new monaco.Range( const lineColumn = await props.plugin.call('offsetToLineColumnConverter', 'offsetToLineColumn',
0, position,
0, position.file,
0, compilationResult.getSourceCode().sources,
4 compilationResult.getAsts())
), console.log(position, fileTarget, lineColumn)
uri: monaco.Uri.parse('test2.sol'), try {
}] props.plugin.call('editor', 'addModel', fileTarget, fileContent)
} catch (e) {
}
const range = new monaco.Range(lineColumn.start.line + 1, lineColumn.start.column + 1, lineColumn.end.line + 1, lineColumn.end.column + 1)
references.push({
range,
uri: monaco.Uri.parse(fileTarget)
})
}
}
}
return references
} }
}) })
@ -463,22 +475,39 @@ export const EditorUI = (props: EditorUIProps) => {
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) {
//console.log(position) console.log('--------------------')
const cursorPosition = props.editorAPI.getHoverPosition(position) const cursorPosition = props.editorAPI.getHoverPosition(position)
//console.log(cursorPosition) const nodeDefinition = await props.plugin.call('contextualListener', 'definitionAtPosition', cursorPosition)
const compilationResult = await props.plugin.call('compilerArtefacts', 'getLastCompilationResult') console.log(nodeDefinition)
const file = await props.plugin.call('fileManager', 'file') const contents = []
if (compilationResult && compilationResult.data && compilationResult.data.sources[file]) { if (!nodeDefinition) return null
const nodes = sourceMappingDecoder.nodesAtPosition(null, cursorPosition, compilationResult.data.sources[file]) if (nodeDefinition.absolutePath) {
// console.log(cursorPosition, nodes) const target = await props.plugin.call('fileManager', 'getPathFromUrl', nodeDefinition.absolutePath)
// loop over nodes if (target.file !== nodeDefinition.absolutePath) {
if (nodes && nodes.length) { contents.push({
nodes.forEach((node) => { value: `${target.file}`
const position = sourceMappingDecoder.decode(node.src) })
const fileTarget = compilationResult.getSourceName(position.file) }
// console.log(position, fileTarget) contents.push({
value: `${nodeDefinition.absolutePath}`
}) })
} }
if (nodeDefinition.typeDescriptions && nodeDefinition.nodeType === 'VariableDeclaration') {
contents.push({
value: `${nodeDefinition.typeDescriptions.typeString} ${nodeDefinition.name}`
})
}
else if (nodeDefinition.typeDescriptions && nodeDefinition.nodeType === 'ElementaryTypeName') {
contents.push({
value: `${nodeDefinition.typeDescriptions.typeString}`
})
} else {
contents.push({
value: `${nodeDefinition.nodeType}`
})
}
for (const key in contents) {
contents[key].value = '```remix-solidity\n' + contents[key].value + '\n```'
} }
return { return {
range: new monaco.Range( range: new monaco.Range(
@ -487,9 +516,7 @@ export const EditorUI = (props: EditorUIProps) => {
position.lineNumber, position.lineNumber,
model.getLineMaxColumn(position.lineNumber) model.getLineMaxColumn(position.lineNumber)
), ),
contents: [ contents: contents
]
}; };
} }
}) })

@ -19,7 +19,7 @@ export function EnvironmentUI (props: EnvironmentProps) {
Environment Environment
</label> </label>
<div className="udapp_environment"> <div className="udapp_environment">
<select id="selectExEnvOptions" data-id="settingsSelectEnvOptions" className="form-control udapp_select custom-select" value={props.selectedEnv} onChange={(e) => { handleChangeExEnv(e.target.value) }}> <select id="selectExEnvOptions" data-id="settingsSelectEnvOptions" className="form-control udapp_select custom-select" value={props.selectedEnv || 0} onChange={(e) => { handleChangeExEnv(e.target.value) }}>
{ {
props.providers.providerList.map((provider, index) => props.providers.providerList.map((provider, index) =>
<option id={provider.id} key={index} data-id={provider.dataId} <option id={provider.id} key={index} data-id={provider.dataId}

Loading…
Cancel
Save