-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
54 lines (49 loc) · 1.63 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode')
const tokenize = require("./tokenizer")
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider("gdscript", new GdDocumentFormatter()))
}
// this method is called when your extension is deactivated
function deactivate() { }
class GdDocumentFormatter {
provideDocumentFormattingEdits(document) {
let edits = []
let indentLvl = 0
let oneIndent
let thisIndent
let inString
for (let lineNum = 0; lineNum < document.lineCount; lineNum++) {
let line = document.lineAt(lineNum)
if (inString) {
if (line.text.includes("\"\"\"")) inString = false
continue
}
let tokens = tokenize(line.text)
if (!oneIndent) oneIndent = tokens[0]
if (oneIndent) {
indentLvl = tokens[0].length / oneIndent.length
}
thisIndent = ""
for (let i = 0; i < indentLvl; i++) {
thisIndent += oneIndent
}
tokens.shift()
let newLine = (thisIndent + tokens.join("")).trimEnd()
edits.push(vscode.TextEdit.replace(line.range, newLine))
let lastToken = tokens.pop()
if (lastToken && lastToken.slice(0, 3) === "\"\"\"" && !lastToken.includes("\"\"\"", 3)) inString = true
}
return edits
}
}
module.exports = {
activate,
deactivate
}