@@ -2,7 +2,7 @@ import * as VS from 'vscode'
2
2
import * as Parser from 'tree-sitter'
3
3
4
4
// 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 } } = {
6
6
'go' : createParser ( 'tree-sitter-go' , colorGo ) ,
7
7
'typescript' : createParser ( 'tree-sitter-typescript' , colorTypescript ) ,
8
8
'cpp' : createParser ( 'tree-sitter-cpp' , colorCpp ) ,
@@ -28,7 +28,7 @@ function colorGo(x: Parser.SyntaxNode, editor: VS.TextEditor) {
28
28
}
29
29
scan ( x )
30
30
31
- return { types, fields, functions}
31
+ return { types, fields, functions }
32
32
}
33
33
34
34
function colorTypescript ( x : Parser . SyntaxNode , editor : VS . TextEditor ) {
@@ -50,7 +50,7 @@ function colorTypescript(x: Parser.SyntaxNode, editor: VS.TextEditor) {
50
50
}
51
51
scan ( x )
52
52
53
- return { types, fields, functions}
53
+ return { types, fields, functions }
54
54
}
55
55
56
56
function colorRust ( x : Parser . SyntaxNode , editor : VS . TextEditor ) {
@@ -76,7 +76,7 @@ function colorRust(x: Parser.SyntaxNode, editor: VS.TextEditor) {
76
76
}
77
77
scan ( x )
78
78
79
- return { types, fields, functions}
79
+ return { types, fields, functions }
80
80
}
81
81
82
82
function colorCpp ( x : Parser . SyntaxNode , editor : VS . TextEditor ) {
@@ -100,12 +100,12 @@ function colorCpp(x: Parser.SyntaxNode, editor: VS.TextEditor) {
100
100
}
101
101
scan ( x )
102
102
103
- return { types, fields, functions}
103
+ return { types, fields, functions }
104
104
}
105
105
106
106
function isVisible ( x : Parser . SyntaxNode , editor : VS . TextEditor ) {
107
107
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
109
109
if ( overlap ) return true
110
110
}
111
111
return false
@@ -115,33 +115,39 @@ function range(x: Parser.SyntaxNode): VS.Range {
115
115
return new VS . Range ( x . startPosition . row , x . startPosition . column , x . endPosition . row , x . endPosition . column )
116
116
}
117
117
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 [ ] }
119
119
120
- function createParser ( module : string , color : ColorFunction ) : { parser : Parser , color : ColorFunction } {
120
+ function createParser ( module : string , color : ColorFunction ) : { parser : Parser , color : ColorFunction } {
121
121
const lang = require ( module )
122
122
const parser = new Parser ( )
123
123
parser . setLanguage ( lang )
124
- return { parser, color}
124
+ return { parser, color }
125
125
}
126
126
127
127
// Called when the extension is first activated by user opening a file with the appropriate language
128
128
export function activate ( context : VS . ExtensionContext ) {
129
129
console . log ( "Activating tree-sitter..." )
130
130
// Parse of all visible documents
131
- const trees : { [ uri : string ] : Parser . Tree } = { }
131
+ const trees : { [ uri : string ] : Parser . Tree } = { }
132
132
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
+ }
138
141
}
139
142
}
140
143
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
+ }
145
151
}
146
152
}
147
153
function updateTree ( parser : Parser , edit : VS . TextDocumentChangeEvent ) {
@@ -156,7 +162,7 @@ export function activate(context: VS.ExtensionContext) {
156
162
const startPosition = asPoint ( startPos )
157
163
const oldEndPosition = asPoint ( oldEndPos )
158
164
const newEndPosition = asPoint ( newEndPos )
159
- const delta = { startIndex, oldEndIndex, newEndIndex, startPosition, oldEndPosition, newEndPosition}
165
+ const delta = { startIndex, oldEndIndex, newEndIndex, startPosition, oldEndPosition, newEndPosition }
160
166
// console.log(edit.document.uri.toString(), delta)
161
167
old . edit ( delta )
162
168
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) {
172
178
return edit . rangeOffset + edit . text . length
173
179
}
174
180
function asPoint ( pos : VS . Position ) : Parser . Point {
175
- return { row : pos . line , column : pos . character }
181
+ return { row : pos . line , column : pos . character }
176
182
}
177
183
function close ( doc : VS . TextDocument ) {
178
184
if ( doc . languageId == 'go' ) {
@@ -181,13 +187,13 @@ export function activate(context: VS.ExtensionContext) {
181
187
}
182
188
// Apply themeable colors
183
189
const typeStyle = VS . window . createTextEditorDecorationType ( {
184
- color : new VS . ThemeColor ( 'treeSitter.type' )
190
+ color : new VS . ThemeColor ( 'treeSitter.type' )
185
191
} )
186
192
const fieldStyle = VS . window . createTextEditorDecorationType ( {
187
- color : new VS . ThemeColor ( 'treeSitter.field' )
193
+ color : new VS . ThemeColor ( 'treeSitter.field' )
188
194
} )
189
195
const functionStyle = VS . window . createTextEditorDecorationType ( {
190
- color : new VS . ThemeColor ( 'treeSitter.function' )
196
+ color : new VS . ThemeColor ( 'treeSitter.function' )
191
197
} )
192
198
function colorUri ( uri : VS . Uri ) {
193
199
for ( let editor of VS . window . visibleTextEditors ) {
@@ -199,12 +205,15 @@ export function activate(context: VS.ExtensionContext) {
199
205
function colorEditor ( editor : VS . TextEditor ) {
200
206
const t = trees [ editor . document . uri . toString ( ) ]
201
207
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
+ }
208
217
// console.log(t.rootNode.toString())
209
218
}
210
219
VS . window . visibleTextEditors . forEach ( open )
@@ -215,4 +224,4 @@ export function activate(context: VS.ExtensionContext) {
215
224
}
216
225
217
226
// this method is called when your extension is deactivated
218
- export function deactivate ( ) { }
227
+ export function deactivate ( ) { }
0 commit comments