|
|
|
@ -4,6 +4,11 @@ import { sourceMappingDecoder } from '@remix-project/remix-debug' |
|
|
|
|
import { CompilerAbstract } from '@remix-project/remix-solidity' |
|
|
|
|
import { Compiler } from '@remix-project/remix-solidity' |
|
|
|
|
|
|
|
|
|
import { helper } from '@remix-project/remix-solidity' |
|
|
|
|
import type { CompilationError } from '@remix-project/remix-solidity-ts' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const profile = { |
|
|
|
|
name: 'contextualListener', |
|
|
|
|
methods: ['getBlockName', 'getAST', 'nodesWithScope', 'getNodes', 'compile', 'getNodeById', 'getLastCompilationResult', 'positionOfDefinition', 'definitionAtPosition', 'jumpToDefinition', 'referrencesAtPosition', 'nodesAtEditorPosition', 'referencesOf', 'getActiveHighlights', 'gasEstimation', 'declarationOf', 'jumpToPosition'], |
|
|
|
@ -59,12 +64,16 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
|
|
|
|
|
async onActivation() { |
|
|
|
|
this.on('editor', 'contentChanged', async () => { |
|
|
|
|
console.log('contentChanged') |
|
|
|
|
await this.getAST() |
|
|
|
|
await this.compile() |
|
|
|
|
this._stopHighlighting() |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
this.on('solidity', 'loadingCompiler', async(url) => { |
|
|
|
|
console.log('loading compiler', url) |
|
|
|
|
|
|
|
|
|
this.compiler.event.register('compilerLoaded', async() => await this.compile() ) |
|
|
|
|
this.compiler.loadVersion(true, url) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
@ -73,7 +82,33 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.onAstFinished = async (success, data, source, input, version) => { |
|
|
|
|
|
|
|
|
|
console.log('compile success', success) |
|
|
|
|
this.call('editor', 'clearAnnotations') |
|
|
|
|
let noFatalErrors = true // ie warnings are ok
|
|
|
|
|
const checkIfFatalError = (error: CompilationError) => { |
|
|
|
|
// Ignore warnings and the 'Deferred import' error as those are generated by us as a workaround
|
|
|
|
|
const isValidError = (error.message && error.message.includes('Deferred import')) ? false : error.severity !== 'warning' |
|
|
|
|
if (isValidError) { |
|
|
|
|
console.log(error) |
|
|
|
|
noFatalErrors = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (data.error) checkIfFatalError(data.error) |
|
|
|
|
if (data.errors) data.errors.forEach((err) => checkIfFatalError(err)) |
|
|
|
|
if (data.errors) { |
|
|
|
|
for (const error of data.errors) { |
|
|
|
|
let pos = helper.getPositionDetails(error.formattedMessage) |
|
|
|
|
if (pos.errFile) { |
|
|
|
|
pos = { |
|
|
|
|
row: pos.errLine, |
|
|
|
|
column: pos.errCol, |
|
|
|
|
text: error.formattedMessage, |
|
|
|
|
type: error.severity |
|
|
|
|
} |
|
|
|
|
await this.call('editor', 'addAnnotation', pos, pos.errFile) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (!data.sources) return |
|
|
|
|
if (data.sources && Object.keys(data.sources).length === 0) return |
|
|
|
|
this.lastCompilationResult = new CompilerAbstract('soljson', data, source, input) |
|
|
|
@ -91,8 +126,10 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
|
|
|
|
|
setInterval(async () => { |
|
|
|
|
|
|
|
|
|
await this.compile() |
|
|
|
|
}, 5000) |
|
|
|
|
//await this.compile()
|
|
|
|
|
}, 1000) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setInterval(async () => { |
|
|
|
|
const compilationResult = this.lastCompilationResult // await this.call('compilerArtefacts', 'getLastCompilationResult')
|
|
|
|
@ -118,6 +155,7 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async compile() { |
|
|
|
|
console.log('compile') |
|
|
|
|
try { |
|
|
|
|
const state = await this.call('solidity', 'getCompilerState') |
|
|
|
|
this.compiler.set('optimize', state.optimize) |
|
|
|
@ -131,6 +169,7 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
const sources = { [this.currentFile]: { content } } |
|
|
|
|
this.compiler.compile(sources, this.currentFile) |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -159,7 +198,7 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
async getAST(text: string = null) { |
|
|
|
|
this.currentFile = await this.call('fileManager', 'file') |
|
|
|
|
if (!this.currentFile) return |
|
|
|
|
let fileContent = text || await this.call('fileManager', 'readFile', this.currentFile) |
|
|
|
|
const fileContent = text || await this.call('fileManager', 'readFile', this.currentFile) |
|
|
|
|
try { |
|
|
|
|
const ast = (SolidityParser as any).parse(fileContent, { loc: true, range: true, tolerant: true }) |
|
|
|
|
this.lastAST = ast |
|
|
|
@ -207,7 +246,7 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
async nodesAtEditorPosition(position: any, type: string = '') { |
|
|
|
|
const lastCompilationResult = this.lastCompilationResult // await this.call('compilerArtefacts', 'getLastCompilationResult')
|
|
|
|
|
if (!lastCompilationResult) return false |
|
|
|
|
let urlFromPath = await this.call('fileManager', 'getUrlFromPath', this.currentFile) |
|
|
|
|
const urlFromPath = await this.call('fileManager', 'getUrlFromPath', this.currentFile) |
|
|
|
|
if (lastCompilationResult && lastCompilationResult.languageversion.indexOf('soljson') === 0 && lastCompilationResult.data) { |
|
|
|
|
const nodes = sourceMappingDecoder.nodesAtPosition(type, position, lastCompilationResult.data.sources[this.currentFile] || lastCompilationResult.data.sources[urlFromPath.file]) |
|
|
|
|
return nodes |
|
|
|
@ -326,7 +365,7 @@ export class EditorContextListener extends Plugin { |
|
|
|
|
this._stopHighlighting() |
|
|
|
|
this.currentPosition = cursorPosition |
|
|
|
|
this.currentFile = file |
|
|
|
|
let urlFromPath = await this.call('fileManager', 'getUrlFromPath', this.currentFile) |
|
|
|
|
const urlFromPath = await this.call('fileManager', 'getUrlFromPath', this.currentFile) |
|
|
|
|
if (compilationResult && compilationResult.data && (compilationResult.data.sources[file] || compilationResult.data.sources[urlFromPath.file])) { |
|
|
|
|
const nodes = sourceMappingDecoder.nodesAtPosition(null, cursorPosition, compilationResult.data.sources[file] || compilationResult.data.sources[urlFromPath.file]) |
|
|
|
|
this.nodes = nodes |
|
|
|
|