Skip to content

Commit 8a492b6

Browse files
committed
add types to doctrings
1 parent ce68b3a commit 8a492b6

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

lib_src/misc/blocks.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
import { forLines } from "./scopes"
66
import { TextEditor, Selection } from "atom"
77

8-
interface LineInfo {
9-
scope: readonly string[]
10-
line: string
11-
}
8+
/**
9+
* interface LineInfo {
10+
* scope: readonly string[]
11+
* line: string
12+
* }
13+
*/
1214

15+
/**
16+
*
17+
* @param {TextEditor} editor
18+
* @param {number} l
19+
* @returns {LineInfo}
20+
*/
1321
export function getLine(editor: TextEditor, l: number): LineInfo {
1422
return {
1523
scope: editor.scopeDescriptorForBufferPosition([l, 0]).getScopesArray(),
@@ -20,6 +28,12 @@ export function getLine(editor: TextEditor, l: number): LineInfo {
2028
}
2129
}
2230

31+
/**
32+
*
33+
* @param {LineInfo.line} line
34+
* @param {LineInfo.scope} scope
35+
* @param {boolean} allowDocstrings
36+
*/
2337
function isBlank({ line, scope }: LineInfo, allowDocstrings = false) {
2438
for (const s of scope) {
2539
if (/\bcomment\b/.test(s) || (!allowDocstrings && /\bdocstring\b/.test(s))) {
@@ -29,18 +43,30 @@ function isBlank({ line, scope }: LineInfo, allowDocstrings = false) {
2943
return /^\s*(#.*)?$/.test(line)
3044
}
3145

46+
/**
47+
*
48+
* @param {LineInfo} lineInfo
49+
*/
3250
function isEnd(lineInfo: LineInfo) {
3351
if (isStringEnd(lineInfo)) {
3452
return true
3553
}
3654
return /^(end\b|\)|]|})/.test(lineInfo.line)
3755
}
3856

57+
/**
58+
*
59+
* @param {LineInfo} lineInfo
60+
*/
3961
function isStringEnd(lineInfo: LineInfo) {
4062
const scope = lineInfo.scope.join(" ")
4163
return /\bstring\.multiline\.end\b/.test(scope) || (/\bstring\.end\b/.test(scope) && /\bbacktick\b/.test(scope))
4264
}
4365

66+
/**
67+
*
68+
* @param {LineInfo} lineInfo
69+
*/
4470
function isCont(lineInfo: LineInfo) {
4571
const scope = lineInfo.scope.join(" ")
4672
if (/\bstring\b/.test(scope) && !/\bpunctuation\.definition\.string\b/.test(scope)) {
@@ -50,18 +76,31 @@ function isCont(lineInfo: LineInfo) {
5076
return lineInfo.line.match(/^(else|elseif|catch|finally)\b/)
5177
}
5278

79+
/**
80+
*
81+
* @param {LineInfo} lineInfo
82+
*/
5383
function isStart(lineInfo: LineInfo) {
5484
return !(/^\s/.test(lineInfo.line) || isBlank(lineInfo) || isEnd(lineInfo) || isCont(lineInfo))
5585
}
5686

57-
87+
/**
88+
*
89+
* @param {TextEditor} editor
90+
* @param {number} row
91+
*/
5892
function walkBack(editor: TextEditor, row: number) {
5993
while (row > 0 && !isStart(getLine(editor, row))) {
6094
row--
6195
}
6296
return row
6397
}
6498

99+
/**
100+
*
101+
* @param {TextEditor} editor
102+
* @param {number} start
103+
*/
65104
function walkForward(editor: TextEditor, start: number) {
66105
let end = start
67106
let mark = start
@@ -86,6 +125,12 @@ function walkForward(editor: TextEditor, start: number) {
86125
return end
87126
}
88127

128+
/**
129+
*
130+
* @param {TextEditor} editor
131+
* @param {number} row
132+
* @returns {[[number, number], [number, number]] | undefined}
133+
*/
89134
function getRange(editor: TextEditor, row: number): [[number, number], [number, number]] | undefined {
90135
const start = walkBack(editor, row)
91136
const end = walkForward(editor, start)
@@ -99,6 +144,11 @@ function getRange(editor: TextEditor, row: number): [[number, number], [number,
99144
}
100145
}
101146

147+
/**
148+
*
149+
* @param {TextEditor} editor
150+
* @param {Selection} selection
151+
*/
102152
function getSelection(editor: TextEditor, selection: Selection) {
103153
const { start, end } = selection.getBufferRange()
104154
const range = [
@@ -116,6 +166,12 @@ function getSelection(editor: TextEditor, selection: Selection) {
116166
return range
117167
}
118168

169+
/**
170+
*
171+
* @param {TextEditor} editor
172+
* @param {Selection} selection
173+
* @param {[[number, number], [number, number]]} range
174+
*/
119175
export function moveNext(editor: TextEditor, selection: Selection, range: [[number, number], [number, number]]) {
120176
// Ensure enough room at the end of the buffer
121177
const row = range[1][0]
@@ -142,6 +198,10 @@ export function moveNext(editor: TextEditor, selection: Selection, range: [[numb
142198
])
143199
}
144200

201+
/**
202+
*
203+
* @param {TextEditor} editor
204+
*/
145205
function getRanges(editor: TextEditor) {
146206
const ranges = editor.getSelections().map(selection => {
147207
return {
@@ -157,6 +217,10 @@ function getRanges(editor: TextEditor) {
157217
})
158218
}
159219

220+
/**
221+
*
222+
* @param {TextEditor} editor
223+
*/
160224
export function get(editor: TextEditor) {
161225
return getRanges(editor).map(({ range, selection }) => {
162226
return {
@@ -168,6 +232,11 @@ export function get(editor: TextEditor) {
168232
})
169233
}
170234

235+
/**
236+
*
237+
* @param {TextEditor} editor
238+
* @param {number} row
239+
*/
171240
export function getLocalContext(editor: TextEditor, row: number) {
172241
const range = getRange(editor, row)
173242
const context = range ? editor.getTextInBufferRange(range) : ""
@@ -182,6 +251,10 @@ export function getLocalContext(editor: TextEditor, row: number) {
182251
}
183252
}
184253

254+
/**
255+
*
256+
* @param {TextEditor | undefined} editor
257+
*/
185258
export function select(editor = atom.workspace.getActiveTextEditor()) {
186259
if (!editor) return
187260
return editor.mutateSelectedText(selection => {

0 commit comments

Comments
 (0)