From 9fb6e1f6422f0d2e7ca2482e44fa243d772a7ed1 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 4 May 2020 16:29:57 +0200 Subject: [PATCH] allow multiple highlight per plugin --- src/app/editor/SourceHighlighters.js | 25 +++++++++++--- src/app/editor/editor.js | 12 ++++++- src/app/editor/sourceHighlighter.js | 3 +- test-browser/tests/editor.test.js | 50 ++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/app/editor/SourceHighlighters.js b/src/app/editor/SourceHighlighters.js index bf44e50c27..7560d2832b 100644 --- a/src/app/editor/SourceHighlighters.js +++ b/src/app/editor/SourceHighlighters.js @@ -12,16 +12,33 @@ class SourceHighlighters { highlight (position, filePath, hexColor, from) { try { - if (!this.highlighters[from]) this.highlighters[from] = new SourceHighlighter() - this.highlighters[from].currentSourceLocation(null) - this.highlighters[from].currentSourceLocationFromfileName(position, filePath, hexColor) + if (!this.highlighters[from]) this.highlighters[from] = [] + const sourceHighlight = new SourceHighlighter() + sourceHighlight.currentSourceLocationFromfileName(position, filePath, hexColor) + this.highlighters[from].push(sourceHighlight) } catch (e) { throw e } } discardHighlight (from) { - if (this.highlighters[from]) this.highlighters[from].currentSourceLocation(null) + if (this.highlighters[from]) { + for (const index in this.highlighters[from]) this.highlighters[from][index].currentSourceLocation(null) + } + this.highlighters[from] = [] + } + + discardHighlightAt (line, filePath, from) { + if (this.highlighters[from]) { + for (const index in this.highlighters[from]) { + const highlight = this.highlighters[from][index] + if (highlight.source === filePath && + (highlight.position.start.line === line || highlight.position.end.line === line)) { + highlight.currentSourceLocation(null) + this.highlighters[from].splice(index, 1) + } + } + } } } diff --git a/src/app/editor/editor.js b/src/app/editor/editor.js index f882e99a9e..369eee5a0e 100644 --- a/src/app/editor/editor.js +++ b/src/app/editor/editor.js @@ -46,7 +46,7 @@ const profile = { name: 'editor', description: 'service - editor', version: packageJson.version, - methods: ['highlight', 'discardHighlight', 'clearAnnotations', 'addAnnotation'] + methods: ['highlight', 'discardHighlight', 'discardHighlightAt', 'clearAnnotations', 'addAnnotation'] } class Editor extends Plugin { @@ -202,6 +202,16 @@ class Editor extends Plugin { this.sourceHighlighters.discardHighlight(from) } + discardHighlightAt (line, filePath) { + const { from } = this.currentRequest + this.sourceHighlighters.discardHighlightAt(line, filePath, from) + } + + currentHighlights () { + const { from } = this.currentRequest + return this.sourceHighlighters.currentHighlights(from) + } + setTheme (type) { this.editor.setTheme('ace/theme/' + this._themes[type]) } diff --git a/src/app/editor/sourceHighlighter.js b/src/app/editor/sourceHighlighter.js index 2bd6b2fb86..daec7811f1 100644 --- a/src/app/editor/sourceHighlighter.js +++ b/src/app/editor/sourceHighlighter.js @@ -13,6 +13,7 @@ class SourceHighlighter { fileManager: this._components.registry.get('filemanager').api, compilerArtefacts: this._components.registry.get('compilersartefacts').api } + this.position = null this.statementMarker = null this.fullLineMarker = null this.source = null @@ -61,7 +62,7 @@ class SourceHighlighter { this.statementMarker = this._deps.editor.addMarker(lineColumnPos, this.source, css.highlightcode.className + ' ' + css.customBackgroundColor.className) this._deps.editor.scrollToLine(lineColumnPos.start.line, true, true, function () {}) - + this.position = lineColumnPos if (lineColumnPos.start.line === lineColumnPos.end.line) { this.fullLineMarker = this._deps.editor.addMarker({ start: { diff --git a/test-browser/tests/editor.test.js b/test-browser/tests/editor.test.js index 2cc2e3fd21..bd2e04769e 100644 --- a/test-browser/tests/editor.test.js +++ b/test-browser/tests/editor.test.js @@ -70,6 +70,12 @@ module.exports = { .checkElementStyle('.ace_comment.ace_doc', 'color', aceThemes.dark.comment) .checkElementStyle('.ace_function', 'color', aceThemes.dark.function) .checkElementStyle('.ace_variable', 'color', aceThemes.dark.variable) + }, + + 'Should highlight source code': function (browser) { + browser.addFile('browser/sourcehighlight.js', sourcehighlightScript) + .switchFile('browser/sourcehighlight.js') + .executeScript('remix.exeCurrent()') .end() }, @@ -90,3 +96,47 @@ var aceThemes = { variable: 'rgb(153, 119, 68)' } } + +const sourcehighlightScript = ` +(async () => { + try { + const pos = { + start: { + line: 32, + column: 3 + }, + end: { + line: 32, + column: 20 + } + } + await remix.call('editor', 'highlight', pos, 'browser/3_Ballot.sol') + + const pos2 = { + start: { + line: 40, + column: 3 + }, + end: { + line: 40, + column: 20 + } + } + await remix.call('editor', 'highlight', pos2, 'browser/3_Ballot.sol') + + const pos3 = { + start: { + line: 50, + column: 3 + }, + end: { + line: 50, + column: 20 + } + } + await remix.call('editor', 'highlight', pos3, 'browser/3_Ballot.sol') + } catch (e) { + console.log(e.message) + } +})() +`