Skip to content

Commit fc60500

Browse files
committed
misc/blocks type definitions + prettier
misc/blocks type definitions Update blocks.ts
1 parent 3db3343 commit fc60500

File tree

2 files changed

+87
-63
lines changed

2 files changed

+87
-63
lines changed

lib_src/misc/blocks.ts

Lines changed: 86 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,72 @@
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

4-
import { forLines } from './scopes'
5+
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([
17+
[l, 0],
18+
[l, Infinity]
19+
])
1020
}
1121
}
1222

13-
function isBlank ({line, scope}, allowDocstrings = false) {
23+
function isBlank({ line, scope }: LineInfo, allowDocstrings = false) {
1424
for (const s of scope) {
1525
if (/\bcomment\b/.test(s) || (!allowDocstrings && /\bdocstring\b/.test(s))) {
1626
return true
1727
}
1828
}
19-
return /^\s*(#.*)?$/.test(line);
29+
return /^\s*(#.*)?$/.test(line)
2030
}
21-
function isEnd ({ line, scope }) {
22-
if (isStringEnd({ line, scope })) {
31+
32+
function isEnd(lineInfo: LineInfo) {
33+
if (isStringEnd(lineInfo)) {
2334
return true
2435
}
25-
return /^(end\b|\)|\]|\})/.test(line);
36+
return /^(end\b|\)|\]|\})/.test(lineInfo.line)
2637
}
27-
function isStringEnd ({ line, scope }) {
28-
scope = scope.join(' ')
29-
return /\bstring\.multiline\.end\b/.test(scope) ||
30-
(/\bstring\.end\b/.test(scope) && /\bbacktick\b/.test(scope));
38+
39+
function isStringEnd(lineInfo: LineInfo) {
40+
const scope = lineInfo.scope.join(" ")
41+
return /\bstring\.multiline\.end\b/.test(scope) || (/\bstring\.end\b/.test(scope) && /\bbacktick\b/.test(scope))
3142
}
32-
function isCont ({ line, scope }) {
33-
scope = scope.join(' ')
34-
if (/\bstring\b/.test(scope) && !(/\bpunctuation\.definition\.string\b/.test(scope))) {
43+
44+
function isCont(lineInfo: LineInfo) {
45+
const scope = lineInfo.scope.join(" ")
46+
if (/\bstring\b/.test(scope) && !/\bpunctuation\.definition\.string\b/.test(scope)) {
3547
return true
3648
}
3749

38-
return line.match(/^(else|elseif|catch|finally)\b/);
50+
return lineInfo.line.match(/^(else|elseif|catch|finally)\b/)
3951
}
40-
function isStart (lineInfo) {
41-
return !(/^\s/.test(lineInfo.line) || isBlank(lineInfo) || isEnd(lineInfo) || isCont(lineInfo));
52+
53+
function isStart(lineInfo: LineInfo) {
54+
return !(/^\s/.test(lineInfo.line) || isBlank(lineInfo) || isEnd(lineInfo) || isCont(lineInfo))
4255
}
4356

44-
function walkBack(ed, row) {
45-
while ((row > 0) && !isStart(getLine(ed, row))) {
57+
function walkBack(editor: TextEditor, row: number) {
58+
while (row > 0 && !isStart(getLine(editor, row))) {
4659
row--
4760
}
4861
return row
4962
}
5063

51-
function walkForward (ed, start) {
64+
function walkForward(editor: TextEditor, start: number) {
5265
let end = start
5366
let mark = start
54-
while (mark < ed.getLastBufferRow()) {
67+
while (mark < editor.getLastBufferRow()) {
5568
mark++
56-
const lineInfo = getLine(ed, mark)
69+
const lineInfo = getLine(editor, mark)
5770

5871
if (isStart(lineInfo)) {
5972
break
@@ -62,10 +75,7 @@ function walkForward (ed, start) {
6275
// An `end` only counts when there still are unclosed blocks (indicated by `forLines`
6376
// returning a non-empty array).
6477
// If the line closes a multiline string we also take that as ending the block.
65-
if (
66-
!(forLines(ed, start, mark-1).length === 0) ||
67-
isStringEnd(lineInfo)
68-
) {
78+
if (!(forLines(editor, start, mark - 1).length === 0) || isStringEnd(lineInfo)) {
6979
end = mark
7080
}
7181
} else if (!(isBlank(lineInfo) || isStart(lineInfo))) {
@@ -75,76 +85,90 @@ function walkForward (ed, start) {
7585
return end
7686
}
7787

78-
function getRange (ed, row) {
79-
const start = walkBack(ed, row)
80-
const end = walkForward(ed, start)
88+
function getRange(editor: TextEditor, row: number): [[number, number], [number, number]] | undefined {
89+
const start = walkBack(editor, row)
90+
const end = walkForward(editor, start)
8191
if (start <= row && row <= end) {
82-
return [[start, 0], [end, Infinity]]
92+
return [
93+
[start, 0],
94+
[end, Infinity]
95+
]
96+
} else {
97+
return undefined // TODO
8398
}
8499
}
85100

86-
function getSelection (ed, sel) {
87-
const {start, end} = sel.getBufferRange()
88-
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])) {
101+
function getSelection(editor: TextEditor, selection: Selection) {
102+
const { start, end } = selection.getBufferRange()
103+
const range = [
104+
[start.row, start.column],
105+
[end.row, end.column]
106+
]
107+
while (isBlank(getLine(editor, range[0][0]), true) && range[0][0] <= range[1][0]) {
90108
range[0][0]++
91109
range[0][1] = 0
92110
}
93-
while (isBlank(getLine(ed, range[1][0]), true) && (range[1][0] >= range[0][0])) {
111+
while (isBlank(getLine(editor, range[1][0]), true) && range[1][0] >= range[0][0]) {
94112
range[1][0]--
95113
range[1][1] = Infinity
96114
}
97115
return range
98116
}
99117

100-
export function moveNext (ed, sel, range) {
118+
export function moveNext(editor: TextEditor, selection: Selection, range: [[number, number], [number, number]]) {
101119
// Ensure enough room at the end of the buffer
102120
const row = range[1][0]
103121
let last
104-
while ((last = ed.getLastBufferRow()) < (row+2)) {
105-
if ((last !== row) && !isBlank(getLine(ed, last))) {
122+
while ((last = editor.getLastBufferRow()) < row + 2) {
123+
if (last !== row && !isBlank(getLine(editor, last))) {
106124
break
107125
}
108-
sel.setBufferRange([[last, Infinity], [last, Infinity]])
109-
sel.insertText('\n')
126+
selection.setBufferRange([
127+
[last, Infinity],
128+
[last, Infinity]
129+
])
130+
selection.insertText("\n")
110131
}
111132
// Move the cursor
112133
let to = row + 1
113-
while ((to < ed.getLastBufferRow()) && isBlank(getLine(ed, to))) {
134+
while (to < editor.getLastBufferRow() && isBlank(getLine(editor, to))) {
114135
to++
115136
}
116-
to = walkForward(ed, to)
117-
return sel.setBufferRange([[to, Infinity], [to, Infinity]])
137+
to = walkForward(editor, to)
138+
return selection.setBufferRange([
139+
[to, Infinity],
140+
[to, Infinity]
141+
])
118142
}
119143

120-
function getRanges (ed) {
121-
const ranges = ed.getSelections().map(sel => {
144+
function getRanges(editor: TextEditor) {
145+
const ranges = editor.getSelections().map(selection => {
122146
return {
123-
selection: sel,
124-
range: sel.isEmpty() ?
125-
getRange(ed, sel.getHeadBufferPosition().row) :
126-
getSelection(ed, sel)
147+
selection: selection,
148+
range: selection.isEmpty()
149+
? getRange(editor, selection.getHeadBufferPosition().row)
150+
: getSelection(editor, selection)
127151
}
128152
})
129153
return ranges.filter(({ range }) => {
130-
return range && ed.getTextInBufferRange(range).trim()
154+
return range && editor.getTextInBufferRange(range).trim()
131155
})
132156
}
133157

134-
export function get (ed) {
135-
return getRanges(ed).map(({ range, selection }) => {
158+
export function get(editor: TextEditor) {
159+
return getRanges(editor).map(({ range, selection }) => {
136160
return {
137161
range,
138162
selection,
139163
line: range[0][0],
140-
text: ed.getTextInBufferRange(range)
164+
text: editor.getTextInBufferRange(range)
141165
}
142166
})
143167
}
144168

145-
export function getLocalContext (editor, row) {
169+
export function getLocalContext(editor: TextEditor, row: number) {
146170
const range = getRange(editor, row)
147-
const context = range ? editor.getTextInBufferRange(range) : ''
171+
const context = range ? editor.getTextInBufferRange(range) : ""
148172
// NOTE:
149173
// backend code expects startRow to be number for most cases, e.g.: `row = row - startRow`
150174
// so let's just return `0` when there is no local context
@@ -156,10 +180,10 @@ export function getLocalContext (editor, row) {
156180
}
157181
}
158182

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)
183+
export function select(editor = atom.workspace.getActiveTextEditor()) {
184+
if (!editor) return
185+
return editor.mutateSelectedText(selection => {
186+
const range = getRange(editor, selection.getHeadBufferPosition().row)
163187
if (range) {
164188
selection.setBufferRange(range)
165189
}

lib_src/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
// "skipLibCheck": false,
3434
"outDir": "../lib/misc"
3535
},
36-
"files" : ["misc/scopes.ts"],
36+
"files" : ["misc/blocks.ts"],
3737
"compileOnSave": true
3838
}

0 commit comments

Comments
 (0)