Skip to content

Commit 63d19d2

Browse files
committed
npm run build
1 parent ce68b3a commit 63d19d2

File tree

2 files changed

+95
-58
lines changed

2 files changed

+95
-58
lines changed

lib/misc/blocks.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TextEditor, Selection } from "atom";
2+
interface LineInfo {
3+
scope: readonly string[];
4+
line: string;
5+
}
6+
export declare function getLine(editor: TextEditor, l: number): LineInfo;
7+
export declare function moveNext(editor: TextEditor, selection: Selection, range: [[number, number], [number, number]]): void;
8+
export declare function get(editor: TextEditor): {
9+
range: number[][] | undefined;
10+
selection: Selection;
11+
line: number;
12+
text: any;
13+
}[];
14+
export declare function getLocalContext(editor: TextEditor, row: number): {
15+
context: string;
16+
startRow: number;
17+
};
18+
export declare function select(editor?: TextEditor | undefined): void;
19+
export {};

lib/misc/blocks.js

Lines changed: 76 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
11
'use babel'
22
// TODO: docstrings
3-
3+
// TODO: Fix RangeCompatible types
44
import { forLines } from './scopes'
55

6-
export function getLine (ed, l) {
6+
export function getLine(editor, l) {
77
return {
8-
scope: ed.scopeDescriptorForBufferPosition([l, 0]).scopes,
9-
line: ed.getTextInBufferRange([[l, 0], [l, Infinity]])
8+
scope: editor.scopeDescriptorForBufferPosition([l, 0]).getScopesArray(),
9+
line: editor.getTextInBufferRange([
10+
[l, 0],
11+
[l, Infinity]
12+
])
1013
}
1114
}
1215

13-
function isBlank ({line, scope}, allowDocstrings = false) {
16+
function isBlank({ line, scope }, allowDocstrings = false) {
1417
for (const s of scope) {
1518
if (/\bcomment\b/.test(s) || (!allowDocstrings && /\bdocstring\b/.test(s))) {
1619
return true
1720
}
1821
}
1922
return /^\s*(#.*)?$/.test(line)
2023
}
21-
function isEnd ({ line, scope }) {
22-
if (isStringEnd({ line, scope })) {
24+
25+
function isEnd(lineInfo) {
26+
if (isStringEnd(lineInfo)) {
2327
return true
2428
}
25-
return /^(end\b|\)|\]|\})/.test(line)
29+
return /^(end\b|\)|]|})/.test(lineInfo.line)
2630
}
27-
function isStringEnd ({ line, scope }) {
28-
scope = scope.join(' ')
31+
32+
function isStringEnd(lineInfo) {
33+
const scope = lineInfo.scope.join(' ')
2934
return /\bstring\.multiline\.end\b/.test(scope) ||
3035
(/\bstring\.end\b/.test(scope) && /\bbacktick\b/.test(scope))
3136
}
32-
function isCont ({ line, scope }) {
33-
scope = scope.join(' ')
34-
if (/\bstring\b/.test(scope) && !(/\bpunctuation\.definition\.string\b/.test(scope))) {
37+
38+
function isCont(lineInfo) {
39+
const scope = lineInfo.scope.join(' ')
40+
if (/\bstring\b/.test(scope) && !/\bpunctuation\.definition\.string\b/.test(scope)) {
3541
return true
3642
}
37-
38-
return line.match(/^(else|elseif|catch|finally)\b/)
43+
return lineInfo.line.match(/^(else|elseif|catch|finally)\b/)
3944
}
40-
function isStart (lineInfo) {
45+
46+
function isStart(lineInfo) {
4147
return !(/^\s/.test(lineInfo.line) || isBlank(lineInfo) || isEnd(lineInfo) || isCont(lineInfo))
4248
}
4349

44-
function walkBack(ed, row) {
45-
while ((row > 0) && !isStart(getLine(ed, row))) {
50+
function walkBack(editor, row) {
51+
while (row > 0 && !isStart(getLine(editor, row))) {
4652
row--
4753
}
4854
return row
4955
}
5056

51-
function walkForward (ed, start) {
57+
function walkForward(editor, start) {
5258
let end = start
5359
let mark = start
54-
while (mark < ed.getLastBufferRow()) {
60+
while (mark < editor.getLastBufferRow()) {
5561
mark++
56-
const lineInfo = getLine(ed, mark)
57-
62+
const lineInfo = getLine(editor, mark)
5863
if (isStart(lineInfo)) {
5964
break
6065
}
6166
if (isEnd(lineInfo)) {
6267
// An `end` only counts when there still are unclosed blocks (indicated by `forLines`
6368
// returning a non-empty array).
6469
// 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-
) {
70+
if (!(forLines(editor, start, mark - 1).length === 0) || isStringEnd(lineInfo)) {
6971
end = mark
7072
}
7173
} else if (!(isBlank(lineInfo) || isStart(lineInfo))) {
@@ -75,74 +77,89 @@ function walkForward (ed, start) {
7577
return end
7678
}
7779

78-
function getRange (ed, row) {
79-
const start = walkBack(ed, row)
80-
const end = walkForward(ed, start)
80+
function getRange(editor, row) {
81+
const start = walkBack(editor, row)
82+
const end = walkForward(editor, start)
8183
if (start <= row && row <= end) {
82-
return [[start, 0], [end, Infinity]]
84+
return [
85+
[start, 0],
86+
[end, Infinity]
87+
]
88+
} else {
89+
return undefined // TODO: make sure returned range from getRanges is not undefined
8390
}
8491
}
8592

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])) {
93+
function getSelection(editor, selection) {
94+
const { start, end } = selection.getBufferRange()
95+
const range = [
96+
[start.row, start.column],
97+
[end.row, end.column]
98+
]
99+
while (isBlank(getLine(editor, range[0][0]), true) && range[0][0] <= range[1][0]) {
90100
range[0][0]++
91101
range[0][1] = 0
92102
}
93-
while (isBlank(getLine(ed, range[1][0]), true) && (range[1][0] >= range[0][0])) {
103+
while (isBlank(getLine(editor, range[1][0]), true) && range[1][0] >= range[0][0]) {
94104
range[1][0]--
95105
range[1][1] = Infinity
96106
}
97107
return range
98108
}
99109

100-
export function moveNext (ed, sel, range) {
110+
export function moveNext(editor, selection, range) {
101111
// Ensure enough room at the end of the buffer
102112
const row = range[1][0]
103113
let last
104-
while ((last = ed.getLastBufferRow()) < (row+2)) {
105-
if ((last !== row) && !isBlank(getLine(ed, last))) {
114+
while ((last = editor.getLastBufferRow()) < row + 2) {
115+
if (last !== row && !isBlank(getLine(editor, last))) {
106116
break
107117
}
108-
sel.setBufferRange([[last, Infinity], [last, Infinity]])
109-
sel.insertText('\n')
118+
selection.setBufferRange([
119+
[last, Infinity],
120+
[last, Infinity]
121+
])
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([
131+
[to, Infinity],
132+
[to, Infinity]
133+
])
118134
}
119135

120-
function getRanges (ed) {
121-
const ranges = ed.getSelections().map(sel => {
136+
function getRanges(editor) {
137+
const ranges = editor.getSelections().map(selection => {
122138
return {
123-
selection: sel,
124-
range: sel.isEmpty() ?
125-
getRange(ed, sel.getHeadBufferPosition().row) :
126-
getSelection(ed, sel)
139+
selection: selection,
140+
range: selection.isEmpty()
141+
? getRange(editor, selection.getHeadBufferPosition().row)
142+
: getSelection(editor, selection)
127143
}
144+
// TODO: replace with getBufferRowRange? (getHeadBufferPosition isn't a public API)
128145
})
129146
return ranges.filter(({ range }) => {
130-
return range && ed.getTextInBufferRange(range).trim()
147+
return range && editor.getTextInBufferRange(range).trim()
131148
})
132149
}
133150

134-
export function get (ed) {
135-
return getRanges(ed).map(({ range, selection }) => {
151+
export function get(editor) {
152+
return getRanges(editor).map(({ range, selection }) => {
136153
return {
137154
range,
138155
selection,
139156
line: range[0][0],
140-
text: ed.getTextInBufferRange(range)
157+
text: editor.getTextInBufferRange(range)
141158
}
142159
})
143160
}
144161

145-
export function getLocalContext (editor, row) {
162+
export function getLocalContext(editor, row) {
146163
const range = getRange(editor, row)
147164
const context = range ? editor.getTextInBufferRange(range) : ''
148165
// NOTE:
@@ -156,12 +173,13 @@ export function getLocalContext (editor, row) {
156173
}
157174
}
158175

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)
176+
export function select(editor = atom.workspace.getActiveTextEditor()) {
177+
if (!editor) return
178+
return editor.mutateSelectedText(selection => {
179+
const range = getRange(editor, selection.getHeadBufferPosition().row)
163180
if (range) {
164181
selection.setBufferRange(range)
165182
}
166183
})
184+
// TODO: replace with getBufferRowRange? (getHeadBufferPosition isn't a public API)
167185
}

0 commit comments

Comments
 (0)