Skip to content

Commit

Permalink
Merge pull request #163 from ndr-brt/first-code-highlight
Browse files Browse the repository at this point in the history
Introduce first version of code highlight
  • Loading branch information
ndr-brt authored Jul 13, 2021
2 parents 8e383f3 + af3a62a commit ff0bf97
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
47 changes: 32 additions & 15 deletions lib/editors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ export class Editors extends EventEmitter {

atom.workspace.observeTextEditors(editor => {
if (this.isTidal(editor)) {
let newLineCount = editor.getLineCount();
this.decorateCodeBlocks(editor);
editor.onDidChange(() => this.decorateCodeBlocks(editor))
this.lineCount = newLineCount;

editor.onDidChange(() => {
let newLineCount = editor.getLineCount();
if (newLineCount !== this.lineCount) {
this.decorateCodeBlocks(editor);
this.lineCount = newLineCount;
}
})
}
})
}
Expand Down Expand Up @@ -49,6 +58,8 @@ export class Editors extends EventEmitter {
return this.getMultiLineExpression(editor);
case WHOLE_EDITOR:
return this.getWholeEditorExpressions(editor);
default:
return this.getWholeEditorExpressions(editor);
}
}
}
Expand Down Expand Up @@ -129,8 +140,9 @@ export class Editors extends EventEmitter {
return blocks
}, [{ rows: [] }])
.filter(block => block.rows.length > 0)
.map(block => {
.map((block, index) => {
return {
id: index,
expression: block.rows.join('\n'),
range: {
start: { row: block.start, column: 0},
Expand Down Expand Up @@ -166,19 +178,24 @@ export class Editors extends EventEmitter {
}

decorateCodeBlocks(editor) {
let newLineCount = editor.getLineCount();
if (newLineCount !== this.lineCount) {
editor.getDecorations({ type: 'line-number' })
.forEach(decoration => decoration.destroy())

this.getWholeEditorExpressions(editor)
.map(it => it.range)
.map(range => [[range.start.row, 0], [range.end.row, 0]])
.map(range => editor.markBufferRange(range, { invalidate: 'never' }))
.map(marker => editor.decorateMarker(marker, { type: 'line-number', class: 'cursor-line' }))

this.lineCount = newLineCount
}
editor.getDecorations({ type: 'line-number' })
.forEach(decoration => decoration.destroy())

this.getWholeEditorExpressions(editor)
.map(it => it.range)
.map(range => [[range.start.row, 0], [range.end.row, 0]])
.map(range => editor.markBufferRange(range, { invalidate: 'never' }))
.map(marker => editor.decorateMarker(marker, { type: 'line-number', class: 'cursor-line' }))
}

currentHighlight(range) {
let editor = currentEditor()

editor.getDecorations({ type: 'text' })
.forEach(decoration => decoration.destroy())

let marker = editor.markBufferRange(range, { invalidate: 'never' })
editor.decorateMarker(marker, { type: 'text', class: 'highlight'})
}

}
Expand Down
17 changes: 12 additions & 5 deletions lib/tidal-listener-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ export default class TidalListenerRepl {
this.consoleView.logPromptErr(message);
break;
case '/code/highlight':
// TODO: handle code highlight
let duration = resultMap.args[0].value
let cyclePosition = resultMap.args[1].value
let startColumn = resultMap.args[2].value
let startRow = resultMap.args[3].value - 1
let stopColumn = resultMap.args[4].value
let stopRow = resultMap.args[5].value - 1
this.editors.currentHighlight([[startRow, startColumn], [stopRow, stopColumn]])
break;
case '/dirt/handshake':
this.consoleView.logPromptErr(`Waiting for SuperDirt`);
Expand All @@ -71,10 +77,10 @@ export default class TidalListenerRepl {
this.tidalSendExpression('hush');
}

tidalSendExpression(expression) {
tidalSendExpression(expression, id) {
var buf = osc.toBuffer({
address: "/code",
args: ["ident", expression] // TODO: ident should be an id
args: [`d${id}`, expression]
});

this.udp.send(buf, 0, buf.length, 6011, "localhost");
Expand All @@ -85,7 +91,8 @@ export default class TidalListenerRepl {
if (!this.editors.currentIsTidal()) return;

if (!this.listener) this.start();
this.editors.currentEvaluations(evalType)
// TODO: it's correct that an eval with listener should eval everything?
this.editors.currentEvaluations()
.filter(eval => eval.expression && eval.range)
.forEach(eval => {
this.status.eval({ characters: eval.expression.length })
Expand All @@ -95,7 +102,7 @@ export default class TidalListenerRepl {
this.editors.copyRange(eval.range);
}

this.tidalSendExpression(eval.expression);
this.tidalSendExpression(eval.expression, eval.id);

if (unflash) {
unflash('eval-success');
Expand Down

0 comments on commit ff0bf97

Please sign in to comment.