forked from lisongx/atom-tidal
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy patheditors.js
199 lines (169 loc) · 5.06 KB
/
editors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use babel'
const EventEmitter = require('events');
import { Point } from 'atom'
export const LINE = 'line'
export const MULTI_LINE = 'multi_line'
export const WHOLE_EDITOR = 'whole_editor'
export class Editors extends EventEmitter {
constructor() {
super();
atom.workspace.observeTextEditors(editor => {
if (this.isTidal(editor)) {
let newLineCount = editor.getLineCount();
this.decorateCodeBlocks(editor);
this.lineCount = newLineCount;
editor.onDidChange(() => {
let newLineCount = editor.getLineCount();
if (newLineCount !== this.lineCount) {
this.decorateCodeBlocks(editor);
this.lineCount = newLineCount;
}
})
}
})
}
currentIsTidal() {
return this.isTidal(currentEditor())
}
isTidal(editor) {
if (!editor) {
return false
} else {
return editor.getGrammar().scopeName === 'source.tidalcycles'
}
}
currentEvaluations(evalType) {
let editor = currentEditor();
if (!editor) return;
var selection = editor.getLastSelection();
var expression = selection.getText();
if (expression) {
var range = selection.getBufferRange();
return [{ expression, range }];
} else {
switch (evalType) {
case LINE:
return this.getLineExpression(editor);
case MULTI_LINE:
return this.getMultiLineExpression(editor);
case WHOLE_EDITOR:
return this.getWholeEditorExpressions(editor);
default:
return this.getWholeEditorExpressions(editor);
}
}
}
evalFlash(range) {
var editor = currentEditor();
var marker = editor.markBufferRange(range, {
invalidate: 'touch'
});
var decoration = editor.decorateMarker(
marker, {
type: 'line',
class: 'eval-flash'
});
// return fn to flash error / success and destroy the flash
return function(cssClass) {
decoration.setProperties({
type: 'line',
class: cssClass
});
setTimeout(() => marker.destroy(), 120);
};
}
copyRange(range) {
var editor = currentEditor();
var endRow = range.end.row;
endRow++
var text = editor.getTextInBufferRange(range);
text = '\n' + text + '\n';
if (endRow > editor.getLastBufferRow()) {
text = '\n' + text
}
editor.getBuffer().insert([endRow, 0], text);
}
goTo(row, column) {
currentEditor().setCursorBufferPosition([row, column])
}
getLineExpression(editor) {
var cursor = editor.getCursors()[0];
var range = cursor.getCurrentLineBufferRange();
var expression = range && editor.getTextInBufferRange(range);
return [{ expression, range }];
}
getMultiLineExpression(editor) {
var range = this.getCurrentParagraphIncludingComments(editor);
var expression = editor.getTextInBufferRange(range);
return [{ expression, range }];
}
getWholeEditorExpressions(editor) {
let wholeEditor = {
start: { row: 0, column: 0 },
end: { row: editor.getLineCount(), column: 0 }
}
let expression = editor.getTextInBufferRange(wholeEditor);
return expression.split('\n')
.reduce((blocks, item, index) => {
if (item.trim().length === 0) {
blocks.push({ rows: [] })
} else {
let block = blocks[blocks.length - 1]
if (block.start === undefined) {
block.start = index
}
block.rows.push(item)
blocks[blocks.length - 1] = block
}
return blocks
}, [{ rows: [] }])
.filter(block => block.rows.length > 0)
.map((block, index) => {
return {
id: `d${index}`,
expression: block.rows.join('\n'),
range: {
start: { row: block.start, column: 0},
end: { row: block.start + block.rows.length, column: 0},
}
}
})
}
getCurrentParagraphIncludingComments(editor) {
var cursor = editor.getLastCursor();
var startRow = cursor.getBufferRow();
var endRow = startRow
var lineCount = editor.getLineCount();
// lines must include non-whitespace characters
// and not be outside editor bounds
while (/\S/.test(editor.lineTextForBufferRow(startRow)) && startRow >= 0) {
startRow--;
}
while (/\S/.test(editor.lineTextForBufferRow(endRow)) && endRow < lineCount) {
endRow++;
}
return {
start: {
row: startRow + 1,
column: 0
},
end: {
row: endRow,
column: 0
},
};
}
decorateCodeBlocks(editor) {
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' }))
}
currentEditor() {
return currentEditor()
}
}
let currentEditor = () => atom.workspace.getActiveTextEditor()