Skip to content

Commit 5407be0

Browse files
Merge pull request #9 from OriginalDecode/fix_exception_invalid_language
Fixed an exception that crashed the extension due to invalid language.
2 parents e4c9595 + f18530e commit 5407be0

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

src/extension.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as VS from 'vscode'
22
import * as Parser from 'tree-sitter'
33

44
// Be sure to declare the language in package.json and include a minimalist grammar.
5-
const languages: {[id: string]: {parser: Parser, color: ColorFunction}} = {
5+
const languages: { [id: string]: { parser: Parser, color: ColorFunction } } = {
66
'go': createParser('tree-sitter-go', colorGo),
77
'typescript': createParser('tree-sitter-typescript', colorTypescript),
88
'cpp': createParser('tree-sitter-cpp', colorCpp),
@@ -28,7 +28,7 @@ function colorGo(x: Parser.SyntaxNode, editor: VS.TextEditor) {
2828
}
2929
scan(x)
3030

31-
return {types, fields, functions}
31+
return { types, fields, functions }
3232
}
3333

3434
function colorTypescript(x: Parser.SyntaxNode, editor: VS.TextEditor) {
@@ -50,7 +50,7 @@ function colorTypescript(x: Parser.SyntaxNode, editor: VS.TextEditor) {
5050
}
5151
scan(x)
5252

53-
return {types, fields, functions}
53+
return { types, fields, functions }
5454
}
5555

5656
function colorRust(x: Parser.SyntaxNode, editor: VS.TextEditor) {
@@ -76,7 +76,7 @@ function colorRust(x: Parser.SyntaxNode, editor: VS.TextEditor) {
7676
}
7777
scan(x)
7878

79-
return {types, fields, functions}
79+
return { types, fields, functions }
8080
}
8181

8282
function colorCpp(x: Parser.SyntaxNode, editor: VS.TextEditor) {
@@ -100,12 +100,12 @@ function colorCpp(x: Parser.SyntaxNode, editor: VS.TextEditor) {
100100
}
101101
scan(x)
102102

103-
return {types, fields, functions}
103+
return { types, fields, functions }
104104
}
105105

106106
function isVisible(x: Parser.SyntaxNode, editor: VS.TextEditor) {
107107
for (let visible of editor.visibleRanges) {
108-
const overlap = x.startPosition.row <= visible.end.line+1 && visible.start.line-1 <= x.endPosition.row
108+
const overlap = x.startPosition.row <= visible.end.line + 1 && visible.start.line - 1 <= x.endPosition.row
109109
if (overlap) return true
110110
}
111111
return false
@@ -115,33 +115,39 @@ function range(x: Parser.SyntaxNode): VS.Range {
115115
return new VS.Range(x.startPosition.row, x.startPosition.column, x.endPosition.row, x.endPosition.column)
116116
}
117117

118-
type ColorFunction = (x: Parser.SyntaxNode, editor: VS.TextEditor) => {types: VS.Range[], fields: VS.Range[], functions: VS.Range[]}
118+
type ColorFunction = (x: Parser.SyntaxNode, editor: VS.TextEditor) => { types: VS.Range[], fields: VS.Range[], functions: VS.Range[] }
119119

120-
function createParser(module: string, color: ColorFunction): {parser: Parser, color: ColorFunction} {
120+
function createParser(module: string, color: ColorFunction): { parser: Parser, color: ColorFunction } {
121121
const lang = require(module)
122122
const parser = new Parser()
123123
parser.setLanguage(lang)
124-
return {parser, color}
124+
return { parser, color }
125125
}
126126

127127
// Called when the extension is first activated by user opening a file with the appropriate language
128128
export function activate(context: VS.ExtensionContext) {
129129
console.log("Activating tree-sitter...")
130130
// Parse of all visible documents
131-
const trees: {[uri: string]: Parser.Tree} = {}
131+
const trees: { [uri: string]: Parser.Tree } = {}
132132
function open(editor: VS.TextEditor) {
133-
const {parser} = languages[editor.document.languageId]
134-
if (parser != null) {
135-
const t = parser.parse(editor.document.getText()) // TODO don't use getText, use Parser.Input
136-
trees[editor.document.uri.toString()] = t
137-
colorUri(editor.document.uri)
133+
const languageSpec = languages[editor.document.languageId]
134+
if (languageSpec != null) {
135+
const { parser } = languageSpec
136+
if (parser != null) {
137+
const t = parser.parse(editor.document.getText()) // TODO don't use getText, use Parser.Input
138+
trees[editor.document.uri.toString()] = t
139+
colorUri(editor.document.uri)
140+
}
138141
}
139142
}
140143
function edit(edit: VS.TextDocumentChangeEvent) {
141-
const {parser} = languages[edit.document.languageId]
142-
if (parser != null) {
143-
updateTree(parser, edit)
144-
colorUri(edit.document.uri)
144+
const languageSpec = languages[edit.document.languageId]
145+
if (languageSpec != null) {
146+
const { parser } = languageSpec
147+
if (parser != null) {
148+
updateTree(parser, edit)
149+
colorUri(edit.document.uri)
150+
}
145151
}
146152
}
147153
function updateTree(parser: Parser, edit: VS.TextDocumentChangeEvent) {
@@ -156,7 +162,7 @@ export function activate(context: VS.ExtensionContext) {
156162
const startPosition = asPoint(startPos)
157163
const oldEndPosition = asPoint(oldEndPos)
158164
const newEndPosition = asPoint(newEndPos)
159-
const delta = {startIndex, oldEndIndex, newEndIndex, startPosition, oldEndPosition, newEndPosition}
165+
const delta = { startIndex, oldEndIndex, newEndIndex, startPosition, oldEndPosition, newEndPosition }
160166
// console.log(edit.document.uri.toString(), delta)
161167
old.edit(delta)
162168
const t = parser.parse(edit.document.getText(), old) // TODO don't use getText, use Parser.Input
@@ -172,7 +178,7 @@ export function activate(context: VS.ExtensionContext) {
172178
return edit.rangeOffset + edit.text.length
173179
}
174180
function asPoint(pos: VS.Position): Parser.Point {
175-
return {row: pos.line, column: pos.character}
181+
return { row: pos.line, column: pos.character }
176182
}
177183
function close(doc: VS.TextDocument) {
178184
if (doc.languageId == 'go') {
@@ -181,13 +187,13 @@ export function activate(context: VS.ExtensionContext) {
181187
}
182188
// Apply themeable colors
183189
const typeStyle = VS.window.createTextEditorDecorationType({
184-
color: new VS.ThemeColor('treeSitter.type')
190+
color: new VS.ThemeColor('treeSitter.type')
185191
})
186192
const fieldStyle = VS.window.createTextEditorDecorationType({
187-
color: new VS.ThemeColor('treeSitter.field')
193+
color: new VS.ThemeColor('treeSitter.field')
188194
})
189195
const functionStyle = VS.window.createTextEditorDecorationType({
190-
color: new VS.ThemeColor('treeSitter.function')
196+
color: new VS.ThemeColor('treeSitter.function')
191197
})
192198
function colorUri(uri: VS.Uri) {
193199
for (let editor of VS.window.visibleTextEditors) {
@@ -199,12 +205,15 @@ export function activate(context: VS.ExtensionContext) {
199205
function colorEditor(editor: VS.TextEditor) {
200206
const t = trees[editor.document.uri.toString()]
201207
if (t == null) return;
202-
const {color} = languages[editor.document.languageId]
203-
if (color == null) return;
204-
const {types, fields, functions} = color(t.rootNode, editor)
205-
editor.setDecorations(typeStyle, types)
206-
editor.setDecorations(fieldStyle, fields)
207-
editor.setDecorations(functionStyle, functions)
208+
const languageSpec = languages[editor.document.languageId]
209+
if (languageSpec != null) {
210+
const { color } = languageSpec
211+
if (color == null) return;
212+
const { types, fields, functions } = color(t.rootNode, editor)
213+
editor.setDecorations(typeStyle, types)
214+
editor.setDecorations(fieldStyle, fields)
215+
editor.setDecorations(functionStyle, functions)
216+
}
208217
// console.log(t.rootNode.toString())
209218
}
210219
VS.window.visibleTextEditors.forEach(open)
@@ -215,4 +224,4 @@ export function activate(context: VS.ExtensionContext) {
215224
}
216225

217226
// this method is called when your extension is deactivated
218-
export function deactivate() {}
227+
export function deactivate() { }

0 commit comments

Comments
 (0)