Skip to content

Commit 4fc0731

Browse files
committed
misc/blocks type definitions
1 parent 0561c55 commit 4fc0731

File tree

1 file changed

+63
-50
lines changed

1 file changed

+63
-50
lines changed

lib_src/misc/blocks.ts

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
1-
'use babel'
21
// TODO: docstrings
2+
// TODO: replace getHeadBufferPosition with getBufferRowRange? - getHeadBufferPosition isn't public API
3+
// TODO: make sure returned range from getRanges is not undefined
34

45
import { forLines } from './scopes'
6+
import { TextEditor, Selection } from "atom"
57

6-
export function getLine (ed, l) {
8+
interface LineInfo {
9+
scope: readonly string[]
10+
line: string
11+
}
12+
13+
export function getLine (editor: TextEditor, l: number): LineInfo {
714
return {
8-
scope: ed.scopeDescriptorForBufferPosition([l, 0]).scopes,
9-
line: ed.getTextInBufferRange([[l, 0], [l, Infinity]])
15+
scope: editor.scopeDescriptorForBufferPosition([l, 0]).getScopesArray(), // was .scopes
16+
line: editor.getTextInBufferRange([[l, 0], [l, Infinity]])
1017
}
1118
}
1219

13-
function isBlank ({line, scope}, allowDocstrings = false) {
20+
function isBlank ({line, scope}: LineInfo, allowDocstrings = false) {
1421
for (const s of scope) {
1522
if (/\bcomment\b/.test(s) || (!allowDocstrings && /\bdocstring\b/.test(s))) {
1623
return true
1724
}
1825
}
1926
return /^\s*(#.*)?$/.test(line);
2027
}
21-
function isEnd ({ line, scope }) {
22-
if (isStringEnd({ line, scope })) {
28+
29+
function isEnd (lineInfo: LineInfo) {
30+
if (isStringEnd(lineInfo)) {
2331
return true
2432
}
25-
return /^(end\b|\)|\]|\})/.test(line);
33+
return /^(end\b|\)|\]|\})/.test(lineInfo.line);
2634
}
27-
function isStringEnd ({ line, scope }) {
28-
scope = scope.join(' ')
35+
36+
function isStringEnd (lineInfo: LineInfo) {
37+
const scope = lineInfo.scope.join(' ')
2938
return /\bstring\.multiline\.end\b/.test(scope) ||
3039
(/\bstring\.end\b/.test(scope) && /\bbacktick\b/.test(scope));
3140
}
32-
function isCont ({ line, scope }) {
33-
scope = scope.join(' ')
41+
42+
function isCont (lineInfo: LineInfo) {
43+
const scope = lineInfo.scope.join(' ')
3444
if (/\bstring\b/.test(scope) && !(/\bpunctuation\.definition\.string\b/.test(scope))) {
3545
return true
3646
}
3747

38-
return line.match(/^(else|elseif|catch|finally)\b/);
48+
return lineInfo.line.match(/^(else|elseif|catch|finally)\b/);
3949
}
40-
function isStart (lineInfo) {
50+
51+
function isStart (lineInfo: LineInfo) {
4152
return !(/^\s/.test(lineInfo.line) || isBlank(lineInfo) || isEnd(lineInfo) || isCont(lineInfo));
4253
}
4354

44-
function walkBack(ed, row) {
45-
while ((row > 0) && !isStart(getLine(ed, row))) {
55+
function walkBack(editor :TextEditor, row: number) {
56+
while ((row > 0) && !isStart(getLine(editor, row))) {
4657
row--
4758
}
4859
return row
4960
}
5061

51-
function walkForward (ed, start) {
62+
function walkForward (editor: TextEditor, start: number) {
5263
let end = start
5364
let mark = start
54-
while (mark < ed.getLastBufferRow()) {
65+
while (mark < editor.getLastBufferRow()) {
5566
mark++
56-
const lineInfo = getLine(ed, mark)
67+
const lineInfo = getLine(editor, mark)
5768

5869
if (isStart(lineInfo)) {
5970
break
@@ -63,7 +74,7 @@ function walkForward (ed, start) {
6374
// returning a non-empty array).
6475
// If the line closes a multiline string we also take that as ending the block.
6576
if (
66-
!(forLines(ed, start, mark-1).length === 0) ||
77+
!(forLines(editor, start, mark-1).length === 0) ||
6778
isStringEnd(lineInfo)
6879
) {
6980
end = mark
@@ -75,74 +86,76 @@ function walkForward (ed, start) {
7586
return end
7687
}
7788

78-
function getRange (ed, row) {
79-
const start = walkBack(ed, row)
80-
const end = walkForward(ed, start)
89+
function getRange (editor: TextEditor, row: number): [[number, number], [number, number]] | undefined {
90+
const start = walkBack(editor, row)
91+
const end = walkForward(editor, start)
8192
if (start <= row && row <= end) {
8293
return [[start, 0], [end, Infinity]]
94+
} else {
95+
return undefined // TODO
8396
}
8497
}
8598

86-
function getSelection (ed, sel) {
87-
const {start, end} = sel.getBufferRange()
99+
function getSelection (editor: TextEditor, selection: Selection) {
100+
const {start, end} = selection.getBufferRange()
88101
const range = [[start.row, start.column], [end.row, end.column]]
89-
while (isBlank(getLine(ed, range[0][0]), true) && (range[0][0] <= range[1][0])) {
102+
while (isBlank(getLine(editor, range[0][0]), true) && (range[0][0] <= range[1][0])) {
90103
range[0][0]++
91104
range[0][1] = 0
92105
}
93-
while (isBlank(getLine(ed, range[1][0]), true) && (range[1][0] >= range[0][0])) {
106+
while (isBlank(getLine(editor, range[1][0]), true) && (range[1][0] >= range[0][0])) {
94107
range[1][0]--
95108
range[1][1] = Infinity
96109
}
97110
return range
98111
}
99112

100-
export function moveNext (ed, sel, range) {
113+
export function moveNext (editor: TextEditor, selection: Selection, range: [[number, number], [number, number]]) {
101114
// Ensure enough room at the end of the buffer
102115
const row = range[1][0]
103116
let last
104-
while ((last = ed.getLastBufferRow()) < (row+2)) {
105-
if ((last !== row) && !isBlank(getLine(ed, last))) {
117+
while ((last = editor.getLastBufferRow()) < (row+2)) {
118+
if ((last !== row) && !isBlank(getLine(editor, last))) {
106119
break
107120
}
108-
sel.setBufferRange([[last, Infinity], [last, Infinity]])
109-
sel.insertText('\n')
121+
selection.setBufferRange([[last, Infinity], [last, Infinity]])
122+
selection.insertText('\n')
110123
}
111124
// Move the cursor
112125
let to = row + 1
113-
while ((to < ed.getLastBufferRow()) && isBlank(getLine(ed, to))) {
126+
while ((to < editor.getLastBufferRow()) && isBlank(getLine(editor, to))) {
114127
to++
115128
}
116-
to = walkForward(ed, to)
117-
return sel.setBufferRange([[to, Infinity], [to, Infinity]])
129+
to = walkForward(editor, to)
130+
return selection.setBufferRange([[to, Infinity], [to, Infinity]])
118131
}
119132

120-
function getRanges (ed) {
121-
const ranges = ed.getSelections().map(sel => {
133+
function getRanges (editor: TextEditor) {
134+
const ranges = editor.getSelections().map(selection => {
122135
return {
123-
selection: sel,
124-
range: sel.isEmpty() ?
125-
getRange(ed, sel.getHeadBufferPosition().row) :
126-
getSelection(ed, sel)
136+
selection: selection,
137+
range: selection.isEmpty() ?
138+
getRange(editor, selection.getHeadBufferPosition().row) :
139+
getSelection(editor, selection)
127140
}
128141
})
129142
return ranges.filter(({ range }) => {
130-
return range && ed.getTextInBufferRange(range).trim()
143+
return range && editor.getTextInBufferRange(range).trim()
131144
})
132145
}
133146

134-
export function get (ed) {
135-
return getRanges(ed).map(({ range, selection }) => {
147+
export function get (editor: TextEditor) {
148+
return getRanges(editor).map(({ range, selection }) => {
136149
return {
137150
range,
138151
selection,
139152
line: range[0][0],
140-
text: ed.getTextInBufferRange(range)
153+
text: editor.getTextInBufferRange(range)
141154
}
142155
})
143156
}
144157

145-
export function getLocalContext (editor, row) {
158+
export function getLocalContext (editor: TextEditor, row: number) {
146159
const range = getRange(editor, row)
147160
const context = range ? editor.getTextInBufferRange(range) : ''
148161
// NOTE:
@@ -156,10 +169,10 @@ export function getLocalContext (editor, row) {
156169
}
157170
}
158171

159-
export function select (ed = atom.workspace.getActiveTextEditor()) {
160-
if (!ed) return
161-
return ed.mutateSelectedText(selection => {
162-
const range = getRange(ed, selection.getHeadBufferPosition().row)
172+
export function select (editor = atom.workspace.getActiveTextEditor()) {
173+
if (!editor) return
174+
return editor.mutateSelectedText(selection => {
175+
const range = getRange(editor, selection.getHeadBufferPosition().row)
163176
if (range) {
164177
selection.setBufferRange(range)
165178
}

0 commit comments

Comments
 (0)