Merge pull request #2934 from ethereum/fixdisablingparser

smarter disabling
pull/2937/head
bunsenstraat 2 years ago committed by GitHub
commit dbebcbe0e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      apps/remix-ide/src/app/plugins/parser/services/code-parser-antlr-service.ts

@ -30,9 +30,9 @@ export default class CodeParserAntlrService {
text: string, text: string,
ast: antlr.ParseResult | null, ast: antlr.ParseResult | null,
duration?: number, duration?: number,
blockDuration?: number,
parsingEnabled?: boolean, parsingEnabled?: boolean,
blocks?: BlockDefinition[] blocks?: BlockDefinition[],
blockDurations?: number[]
} }
} = {}; } = {};
constructor(plugin: CodeParser) { constructor(plugin: CodeParser) {
@ -52,15 +52,15 @@ export default class CodeParserAntlrService {
switch (ev.data.cmd) { switch (ev.data.cmd) {
case 'parsed': case 'parsed':
if (ev.data.ast && self.parserStartTime === ev.data.timestamp) { if (ev.data.ast && self.parserStartTime === ev.data.timestamp) {
self.setFileParsingState(ev.data.file, ev.data.blockDuration)
self.cache[ev.data.file] = { self.cache[ev.data.file] = {
...self.cache[ev.data.file], ...self.cache[ev.data.file],
text: ev.data.text, text: ev.data.text,
ast: ev.data.ast, ast: ev.data.ast,
duration: ev.data.duration, duration: ev.data.duration,
blockDuration: ev.data.blockDuration,
blocks: ev.data.blocks, blocks: ev.data.blocks,
blockDurations: self.cache[ev.data.file].blockDurations? [...self.cache[ev.data.file].blockDurations.slice(-3), ev.data.blockDuration]: [ev.data.blockDuration]
} }
self.setFileParsingState(ev.data.file)
} }
break; break;
} }
@ -68,16 +68,19 @@ export default class CodeParserAntlrService {
}); });
} }
setFileParsingState(file: string, duration: number) { setFileParsingState(file: string) {
if (this.cache[file]) { if (this.cache[file]) {
if (this.cache[file].blockDuration) { if (this.cache[file].blockDurations && this.cache[file].blockDurations.length > 3) {
if (this.cache[file].blockDuration > this.parserTreshHold && duration > this.parserTreshHold) { // calculate average of durations to determine if the parsing should be disabled
const values = [...this.cache[file].blockDurations]
const average = values.reduce((a, b) => a + b, 0) / values.length
if (average > this.parserTreshHold) {
this.cache[file].parsingEnabled = false this.cache[file].parsingEnabled = false
this.plugin.call('notification', 'toast', `This file is big so some autocomplete features will be disabled.`) this.plugin.call('notification', 'toast','Some autocomplete features will be temporarily disabled because the file takes too long to process.')
} else { } else {
this.cache[file].parsingEnabled = true this.cache[file].parsingEnabled = true
} }
} }
} }
} }
@ -127,7 +130,8 @@ export default class CodeParserAntlrService {
this.cache[this.plugin.currentFile] = { this.cache[this.plugin.currentFile] = {
text: '', text: '',
ast: null, ast: null,
parsingEnabled: true parsingEnabled: true,
blockDurations: []
} }
} }
if (this.cache[this.plugin.currentFile] && this.cache[this.plugin.currentFile].text !== fileContent) { if (this.cache[this.plugin.currentFile] && this.cache[this.plugin.currentFile].text !== fileContent) {
@ -244,7 +248,10 @@ export default class CodeParserAntlrService {
try { try {
const startTime = Date.now() const startTime = Date.now()
const blocks = (SolidityParser as any).parseBlock(fileContent, { loc: true, range: true, tolerant: true }) const blocks = (SolidityParser as any).parseBlock(fileContent, { loc: true, range: true, tolerant: true })
this.setFileParsingState(this.plugin.currentFile, Date.now() - startTime) if(this.cache[this.plugin.currentFile] && this.cache[this.plugin.currentFile].blockDurations){
this.cache[this.plugin.currentFile].blockDurations = [...this.cache[this.plugin.currentFile].blockDurations.slice(-3), Date.now() - startTime]
this.setFileParsingState(this.plugin.currentFile)
}
if (blocks) this.cache[this.plugin.currentFile].blocks = blocks if (blocks) this.cache[this.plugin.currentFile].blocks = blocks
return blocks return blocks
} catch (e) { } catch (e) {
@ -279,4 +286,4 @@ export default class CodeParserAntlrService {
return block return block
} }
} }

Loading…
Cancel
Save