forked from russeree/bitcoin-script-hints-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
165 lines (135 loc) · 6.28 KB
/
extension.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as vscode from 'vscode';
import { parseInitialState, processScript } from './parser';
const tokenTypes = new Map<string, number>();
const tokenModifiers = new Map<string, number>();
const legend = new vscode.SemanticTokensLegend([
'function', 'operator', 'keyword', 'parameter', 'variable',
'constant', 'string', 'number', 'comment', 'macro',
'type', 'class', 'enum', 'interface', 'struct'
], ['declaration', 'definition', 'readonly', 'static', 'bitcoin']);
export function activate(context: vscode.ExtensionContext) {
let hintDecorationType = vscode.window.createTextEditorDecorationType({
after: {
color: new vscode.ThemeColor('editorLineNumber.foreground')
}
});
// Register semantic tokens provider
context.subscriptions.push(
vscode.languages.registerDocumentSemanticTokensProvider(
{ language: 'rust' },
new BitcoinScriptSemanticTokensProvider(),
legend
)
);
let activeEditor = vscode.window.activeTextEditor;
function updateDecorations() {
if (!activeEditor || activeEditor.document.languageId !== 'rust') {
return;
}
const text = activeEditor.document.getText();
const hintDecorations: vscode.DecorationOptions[] = [];
// Find all script! macro invocations
const scriptRegex = /script!\s*{([^}]*)}/g;
let match;
while (match = scriptRegex.exec(text)) {
const scriptContent = match[1];
const startPos = activeEditor.document.positionAt(match.index);
const lines = scriptContent.split('\n');
// Find initial state comment
let initialState = null;
let lineOffset = -1;
for (const line of lines) {
if (line.trim().startsWith('//') && line.includes('[')) {
initialState = parseInitialState(line);
break;
}
lineOffset++;
}
if (initialState) {
const hints = processScript(scriptContent, initialState);
hints.forEach((hint, index) => {
// Skip empty hints (non-executed lines)
if (!hint || hint === '') { return; }
const line = lines[lineOffset + index];
if (!line) { return; };
const position = new vscode.Position(
startPos.line + lineOffset + index,
line.length
);
// Skip decoration for NULL hints
if (hint === 'NULL') { return; }
const padding = ' '; // Fixed 4-space padding
hintDecorations.push({
range: new vscode.Range(position, position),
renderOptions: {
after: {
contentText: `${padding} ${hint}`,
color: (hint.includes('ERROR') || hint.includes('Error:')) ? 'red' : undefined
}
}
});
});
}
}
activeEditor.setDecorations(hintDecorationType, hintDecorations);
}
if (activeEditor) {
updateDecorations();
}
vscode.window.onDidChangeActiveTextEditor(editor => {
activeEditor = editor;
if (editor) {
updateDecorations();
}
}, null, context.subscriptions);
vscode.workspace.onDidChangeTextDocument(event => {
if (activeEditor && event.document === activeEditor.document) {
updateDecorations();
}
}, null, context.subscriptions);
}
class BitcoinScriptSemanticTokensProvider implements vscode.DocumentSemanticTokensProvider {
async provideDocumentSemanticTokens(
document: vscode.TextDocument
): Promise<vscode.SemanticTokens> {
const tokensBuilder = new vscode.SemanticTokensBuilder(legend);
const text = document.getText();
// Find all script! macro blocks
const scriptRegex = /script!\s*{([^}]*)}/g;
let match;
while (match = scriptRegex.exec(text)) {
const scriptContent = match[1];
const startPos = document.positionAt(match.index);
// Add semantic tokens for Bitcoin Script operations
const opRegex = /\b(OP_[A-Z0-9]+)\b/g;
let opMatch;
while (opMatch = opRegex.exec(scriptContent)) {
const opCode = opMatch[1];
const opPos = document.positionAt(match.index + opMatch.index);
// Determine token type based on opcode
let tokenType = 'function';
if (opCode.match(/^OP_(DROP|DUP|NIP|OVER|PICK|ROLL|ROT|SWAP|TUCK|2DROP|2DUP|3DUP|2OVER|2ROT|2SWAP|DEPTH|SIZE|TOALTSTACK|FROMALTSTACK|IFDUP)$/)) {
tokenType = 'string';
} else if (opCode.match(/^OP_(IF|NOTIF|ELSE|ENDIF|VERIFY|RETURN|CODESEPARATOR|NOP)$/)) {
tokenType = 'keyword';
} else if (opCode.match(/^OP_(1ADD|1SUB|NEGATE|ABS|NOT|0NOTEQUAL|ADD|SUB|MUL|DIV|MOD|LSHIFT|RSHIFT|MIN|MAX)$/)) {
tokenType = 'number';
} else if (opCode.match(/^OP_(SHA256|SHA1|RIPEMD160|HASH160|HASH256|CHECKSIG|CHECKSIGVERIFY|CHECKMULTISIG|CHECKMULTISIGVERIFY|CHECKSIGADD)$/)) {
tokenType = 'type';
} else if (opCode.match(/^OP_(EQUAL|EQUALVERIFY|BOOLAND|BOOLOR|NUMEQUAL|NUMEQUALVERIFY|NUMNOTEQUAL|LESSTHAN|GREATERTHAN|LESSTHANOREQUAL|GREATERTHANOREQUAL|WITHIN)$/)) {
tokenType = 'operator';
} else if (opCode.match(/^OP_([0-9]|TRUE|FALSE)$/)) {
tokenType = 'constant';
}
tokensBuilder.push(
opPos.line,
opPos.character,
opCode.length,
legend.tokenTypes.indexOf(tokenType),
legend.tokenModifiers.indexOf('bitcoin')
);
}
}
return tokensBuilder.build();
}
}