Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple highlight per plugin #2815

Merged
merged 7 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
allow multiple highlight per plugin
  • Loading branch information
yann300 committed May 7, 2020
commit f465caf26dda89d03948365ecb8cdf21101a28d6
25 changes: 21 additions & 4 deletions src/app/editor/SourceHighlighters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/app/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/editor/sourceHighlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down
50 changes: 50 additions & 0 deletions test-browser/tests/editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},

Expand All @@ -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)
}
})()
`