Skip to content

Commit 9add69c

Browse files
committed
Tweaks
1 parent 93d2abc commit 9add69c

File tree

8 files changed

+97
-83
lines changed

8 files changed

+97
-83
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ node_modules
77
*.map
88
*.vsix
99
*.vsixmanifest
10+
*.textmate
11+
*.tmLanguage
1012

1113
parser.c
1214
grammar.json
1315
node-types.json
1416
parser.exp
1517
parser.lib
1618
parser.obj
17-
scanner.obj
19+
scanner.obj
20+
copyFile.ts

src/DiagnosticCollection.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ function diagnosticsTreeSitterRegexErrors(diagnostics: vscode.Diagnostic[], tree
389389
// vscode.window.showInformationMessage(`Regex ${(performance.now() - start).toFixed(3)}ms`);
390390
}
391391

392-
async function diagnosticsRegularExpressionErrors(diagnostics: vscode.Diagnostic[], trees: trees) {
393-
// vscode.window.showInformationMessage(JSON.stringify("diagnostics Regex Oniguruma"));
392+
function diagnosticsRegularExpressionErrors(diagnostics: vscode.Diagnostic[], trees: trees) {
393+
// vscode.window.showInformationMessage(JSON.stringify("diagnostics Regexes"));
394394
// const start = performance.now();
395395
const regexNodes = trees.regexNodes;
396396

@@ -563,17 +563,16 @@ async function diagnosticsRegularExpressionErrors(diagnostics: vscode.Diagnostic
563563
let errorCodeES: string | undefined;
564564
try {
565565
// https://shiki.style/ uses https://github.com/slevithan/oniguruma-to-es
566-
// There is no escaping backreferences, but aimless backreferences are ignored
567-
566+
// There is no escaping backreferences, but orphan backreferences are ignored
568567

569568
const options: onigurumaToES.ToRegExpOptions = {
570569
accuracy: 'default',
571570
verbose: true,
571+
lazyCompileLength: 0,
572572
rules: {
573573
// Follow `vscode-oniguruma` which enables this Oniguruma option by default
574574
captureGroup: true,
575575
allowOrphanBackrefs: hasBackreferences,
576-
577576
},
578577
};
579578
const jsRegex = onigurumaToES.toRegExpDetails(regex, options);
@@ -653,7 +652,7 @@ async function diagnosticsRegularExpressionErrors(diagnostics: vscode.Diagnostic
653652
});
654653
}
655654
}
656-
// vscode.window.showInformationMessage(`Oniguruma ${(performance.now() - start).toFixed(3)}ms`);
655+
// vscode.window.showInformationMessage(`Regexes ${(performance.now() - start).toFixed(3)}ms`);
657656
}
658657

659658
function diagnosticsBrokenIncludes(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
@@ -802,7 +801,7 @@ function diagnosticsBrokenIncludes(diagnostics: vscode.Diagnostic[], rootNode: w
802801
// vscode.window.showInformationMessage(`include ${(performance.now() - start).toFixed(3)}ms`);
803802
}
804803

805-
async function diagnosticsUnusedRepos(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
804+
function diagnosticsUnusedRepos(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
806805
if (ignoreDiagnosticsUnusedRepos) {
807806
return;
808807
}
@@ -857,7 +856,7 @@ async function diagnosticsUnusedRepos(diagnostics: vscode.Diagnostic[], rootNode
857856
// vscode.window.showInformationMessage(`unusedRepos ${(performance.now() - start).toFixed(3)}ms`);
858857
}
859858

860-
async function diagnosticsDeadTextMateCode(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
859+
function diagnosticsDeadTextMateCode(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
861860
// vscode.window.showInformationMessage(JSON.stringify("diagnostics TextMate dead"));
862861
// const start = performance.now();
863862

@@ -876,7 +875,6 @@ async function diagnosticsDeadTextMateCode(diagnostics: vscode.Diagnostic[], roo
876875
(repo (repository) @repository &begin !match)
877876
(repo (patterns) @patterns &match)
878877
(repo (include) @include &match)
879-
(pattern (include) @include &match)
880878
(repo (include) @include &begin !match)
881879
(repo (include) @include &patterns !begin !match)
882880
(repo [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @begin &match)
@@ -1042,7 +1040,7 @@ async function diagnosticsMismatchingPackageJSONInfo(diagnostics: vscode.Diagnos
10421040
// vscode.window.showInformationMessage(`packageJSON ${(performance.now() - start).toFixed(3)}ms`);
10431041
}
10441042

1045-
async function diagnosticsLinguistCaptures(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
1043+
function diagnosticsLinguistCaptures(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
10461044
// vscode.window.showInformationMessage(JSON.stringify("diagnostics (captures)"))
10471045
// const start = performance.now();
10481046

src/Providers/CompletionItemProvider.ts

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const triggerCharacterSets: { [key: string]: string[]; } = {
2626
};
2727
export const triggerCharacters = Object.values(triggerCharacterSets).flat();
2828

29-
const defaultThemeColors: { [baseTheme: string]: ITextMateThemingRule[]; } = {
29+
const builtInThemeColors: { [baseTheme: string]: ITextMateThemingRule[]; } = {
3030
'light': [
3131
{ scope: 'token.info-token', settings: { foreground: '#316bcd' } },
3232
{ scope: 'token.warn-token', settings: { foreground: '#cd9731' } },
@@ -53,7 +53,7 @@ const defaultThemeColors: { [baseTheme: string]: ITextMateThemingRule[]; } = {
5353
]
5454
};
5555

56-
function comma(cursorNode: webTreeSitter.Node, position: vscode.Position) {
56+
function insertComma(cursorNode: webTreeSitter.Node, position: vscode.Position) {
5757
return toPosition(cursorNode.lastNamedChild!.endPosition).isBefore(position) ? '' : ',';
5858
}
5959

@@ -111,7 +111,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
111111
range: cursorRange,
112112
kind: vscode.CompletionItemKind.Reference,
113113
documentation: "Schema for VSCode's JSON TextMate grammars",
114-
insertText: cursorName == 'schema' ? schema : `"${schema}"${comma(cursorNode, position)}`,
114+
insertText: cursorName == 'schema' ? schema : `"${schema}"${insertComma(cursorNode, position)}`,
115115
sortText: ' ', // top
116116
});
117117
break;
@@ -142,17 +142,20 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
142142
if (Array.isArray(grammars)) {
143143
const documentPath = document.uri.path;
144144
for (const grammar of grammars) {
145-
const grammarPath = vscode.Uri.joinPath(packageUri!, '..', grammar.path).path;
146-
if (grammarPath == documentPath) {
147-
completionItems.push({
148-
label: {
149-
label: grammar.scopeName || `source.${grammar.language}`,
150-
description: grammar.language,
151-
},
152-
documentation: documentPath,
153-
range: cursorRange,
154-
kind: vscode.CompletionItemKind.Variable,
155-
});
145+
const relativePath = grammar.path;
146+
if (typeof relativePath === 'string') {
147+
const absolutePath = vscode.Uri.joinPath(packageUri!, '..', relativePath).path;
148+
if (absolutePath == documentPath) {
149+
completionItems.push({
150+
label: {
151+
label: grammar.scopeName || `source.${grammar.language}`,
152+
description: grammar.language,
153+
},
154+
documentation: documentPath,
155+
range: cursorRange,
156+
kind: vscode.CompletionItemKind.Variable,
157+
});
158+
}
156159
}
157160
}
158161
}
@@ -166,52 +169,55 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
166169
}
167170
const documentPath = document.uri.path;
168171
for (const grammar of grammars) {
169-
const grammarPath = vscode.Uri.joinPath(packageUri!, '..', grammar.path).path;
170-
if (grammarPath == documentPath) {
171-
const languageId = grammar.language;
172-
if (languageId) {
173-
const languages = contributes!.languages;
174-
if (Array.isArray(languages)) {
175-
for (const language of languages) {
176-
if (languageId == language.id) {
177-
const aliases = language.aliases;
178-
if (Array.isArray(aliases)) {
179-
for (const alias of aliases) {
180-
completionItems.push({
181-
label: {
182-
label: alias,
183-
description: grammar.scopeName || languageId,
184-
},
185-
range: cursorRange,
186-
kind: vscode.CompletionItemKind.Text,
187-
});
172+
const relativePath = grammar.path;
173+
if (typeof relativePath === 'string') {
174+
const absolutePath = vscode.Uri.joinPath(packageUri!, '..', relativePath).path;
175+
if (absolutePath == documentPath) {
176+
const languageId = grammar.language;
177+
if (languageId) {
178+
const languages = contributes!.languages;
179+
if (Array.isArray(languages)) {
180+
for (const language of languages) {
181+
if (languageId == language.id) {
182+
const aliases = language.aliases;
183+
if (Array.isArray(aliases)) {
184+
for (const alias of aliases) {
185+
completionItems.push({
186+
label: {
187+
label: alias,
188+
description: grammar.scopeName || languageId,
189+
},
190+
range: cursorRange,
191+
kind: vscode.CompletionItemKind.Text,
192+
});
193+
}
188194
}
189195
}
190196
}
191197
}
192198
}
193-
}
194-
const displayName = packageJSON!.displayName;
195-
if (displayName) {
196-
completionItems.push({
197-
label: {
198-
label: displayName,
199-
description: packageJSON!.name || languageId || grammar.scopeName,
200-
},
201-
range: cursorRange,
202-
kind: vscode.CompletionItemKind.Text,
203-
});
204-
}
205-
const description = packageJSON!.description;
206-
if (description) {
207-
completionItems.push({
208-
label: {
209-
label: description,
210-
description: displayName || packageJSON!.name || languageId || grammar.scopeName,
211-
},
212-
range: cursorRange,
213-
kind: vscode.CompletionItemKind.Text,
214-
});
199+
const displayName = packageJSON!.displayName;
200+
if (displayName) {
201+
completionItems.push({
202+
label: {
203+
label: displayName,
204+
description: packageJSON!.name || languageId || grammar.scopeName,
205+
},
206+
range: cursorRange,
207+
kind: vscode.CompletionItemKind.Text,
208+
});
209+
}
210+
const description = packageJSON!.description;
211+
if (description) {
212+
completionItems.push({
213+
label: {
214+
label: description,
215+
description: displayName || packageJSON!.name || languageId || grammar.scopeName,
216+
},
217+
range: cursorRange,
218+
kind: vscode.CompletionItemKind.Text,
219+
});
220+
}
215221
}
216222
}
217223
}
@@ -259,7 +265,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
259265
range: cursorRange,
260266
kind: vscode.CompletionItemKind.Property,
261267
documentation: documentation,
262-
insertText: cursorName == 'repo' ? text : new vscode.SnippetString(`"${text}": {$0}${comma(cursorNode, position)}`),
268+
insertText: cursorName == 'repo' ? text : new vscode.SnippetString(`"${text}": {$0}${insertComma(cursorNode, position)}`),
263269
});
264270
}
265271
const documentation = new vscode.MarkdownString();
@@ -275,7 +281,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
275281
insertText: cursorName == 'repo' ?
276282
"repo-item" :
277283
new vscode.SnippetString(
278-
`"\${1:repo-item}": {$0}${comma(cursorNode, position)}`
284+
`"\${1:repo-item}": {$0}${insertComma(cursorNode, position)}`
279285
),
280286
sortText: "~repo-item", // bottom
281287
});
@@ -430,10 +436,10 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
430436
'hcDark',
431437
'hcLight',
432438
][vscode.window.activeColorTheme.kind];
433-
const themeRules = defaultThemeColors[baseTheme];
434-
for (const themeRule of themeRules) {
435-
const scope = <string>themeRule.scope;
436-
const settings = themeRule.settings;
439+
const builtInThemeRules = builtInThemeColors[baseTheme];
440+
for (const builtInThemeRule of builtInThemeRules) {
441+
const scope = <string>builtInThemeRule.scope;
442+
const settings = builtInThemeRule.settings;
437443
const documentation = new vscode.MarkdownString();
438444
documentation.appendMarkdown(
439445
`Theme: \`${baseTheme}\` \n` +

src/Providers/DocumentFormattingEditProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const DocumentFormattingEditProvider: vscode.DocumentFormattingEditProvid
3939

4040
export const DocumentRangeFormattingEditProvider: vscode.DocumentRangeFormattingEditProvider = {
4141
provideDocumentRangeFormattingEdits(document: vscode.TextDocument, range: vscode.Range, options: vscode.FormattingOptions, token: vscode.CancellationToken): vscode.TextEdit[] {
42-
// vscode.window.showInformationMessage(JSON.stringify("FormatRange"));
42+
// vscode.window.showInformationMessage(`FormatRange ${JSON.stringify(range)}`);
4343
// const start = performance.now();
4444
const trees = getTrees(document);
4545
const jsonTree = trees.jsonTree;

src/Providers/HoverProvider.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const HoverProvider: vscode.HoverProvider = {
3333
markdownString.appendMarkdown('Regular Expression to match, (capture) and apply `scopeNames` to text \n');
3434
markdownString.appendMarkdown('[VSCode TextMate](https://github.com/microsoft/vscode-textmate) uses the [Oniguruma](https://github.com/kkos/oniguruma/blob/v6.9.8/doc/RE) regex dialect \n');
3535
markdownString.appendMarkdown('[TextMate 2.0](https://macromates.com/) uses the [Onigmo](https://github.com/textmate/Onigmo/blob/Onigmo-5.13.5/doc/RE) regex dialect \n');
36+
markdownString.appendMarkdown('[Github-Linguist](https://github.com/github-linguist/linguist) uses the [PCRE](https://github.com/vmg/libpcre) regex dialect \n');
3637
markdownString.appendCodeblock('Example: \\\\b(true|false)\\\\b', 'json-textmate-regex');
3738
break;
3839
case 'begin':
@@ -79,20 +80,21 @@ export const HoverProvider: vscode.HoverProvider = {
7980
rules: {
8081
// Follow `vscode-oniguruma` which enables this Oniguruma option by default
8182
captureGroup: true,
83+
// end/while rules might reference begin rules, so we allow orphan backreferences
8284
allowOrphanBackrefs: hoverCapture.name == 'while' || hoverCapture.name == 'end',
8385
},
8486
};
8587
const jsRegex = onigurumaToES.toRegExpDetails(text, options);
8688

8789
// markdownString.appendMarkdown('<details><summary>Click to show JS Translation</summary> \n');
8890
markdownString.appendMarkdown('---\n');
89-
markdownString.appendMarkdown('[Translation](https://github.com/slevithan/oniguruma-to-es) from [Oniguruma](https://github.com/kkos/oniguruma/blob/master/doc/RE) to JavaScript: ');
91+
markdownString.appendMarkdown('[Translation](https://github.com/slevithan/oniguruma-to-es) from [Oniguruma](https://github.com/kkos/oniguruma/blob/master/doc/RE) to [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions): ');
9092
markdownString.appendCodeblock(`/${jsRegex.pattern.replaceAll(/\\([\\\/])|(\/)/g, '\\$1$2') || '(?:)'}/${jsRegex.flags}`, 'javascript');
9193
// markdownString.appendMarkdown('</details> \n');
9294
// markdownString.supportHtml = true;
9395
}
9496
} catch (error) {
95-
console.warn("JSON TextMate: oniguruma-to-es:\n", error);
97+
// console.warn("JSON TextMate: oniguruma-to-es:\n", error);
9698
}
9799

98100
const range = toRange(hoverNode);
@@ -104,7 +106,7 @@ export const HoverProvider: vscode.HoverProvider = {
104106

105107
function debugTreeSitterHovers(trees: trees, point: webTreeSitter.Point): vscode.Hover | undefined {
106108
const node = trees.jsonTree.rootNode.descendantForPosition(point);
107-
// const node = jsonTree.rootNode.namedDescendantForPosition(point);
109+
// const node = trees.jsonTree.rootNode.namedDescendantForPosition(point);
108110

109111
if (!node) {
110112
return;

src/Providers/TreeDataProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ export function initCallStackView(context: vscode.ExtensionContext): void {
855855
vscode.commands.registerTextEditorCommand("textmate.callstack", CallStackView),
856856
vscode.commands.registerCommand("textmate.refresh", refresh),
857857
vscode.commands.registerCommand("textmate.find", find),
858-
vscode.commands.registerCommand("textmate.copytoclipboard.grammar", copyToClipBoardGrammar),
858+
vscode.commands.registerCommand("textmate.copytoclipboard.grammar", copyGrammarToClipBoard),
859859
vscode.commands.registerCommand("textmate.call.details", callDetails),
860860
vscode.commands.registerCommand("textmate.goto.file", gotoFile),
861861
vscode.commands.registerCommand("textmate.goto.grammar", gotoGrammar),
@@ -1040,7 +1040,7 @@ async function find(element?: element) {
10401040
// vscode.commands.executeCommand('list.toggleFindMatchType');
10411041
}
10421042

1043-
async function copyToClipBoardGrammar(element?: element) {
1043+
async function copyGrammarToClipBoard(element?: element) {
10441044
try {
10451045
const grammarJSON = JSON.stringify(grammar, stringify);
10461046
await vscode.env.clipboard.writeText(grammarJSON);
@@ -1265,6 +1265,7 @@ async function gotoGrammar(element: element) {
12651265
vscode.commands.executeCommand('editor.action.goToLocations', uri, range.start, locations);
12661266
}
12671267

1268+
// https://github.com/microsoft/vscode-discussions/discussions/2758
12681269
async function gotoFile(element: element) {
12691270
// vscode.window.showInformationMessage(`gotoFile\n${JSON.stringify(element)}`);
12701271

src/TextMate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function parseExtensions() {
4444
const dev = extension.packageJSON.isUnderDevelopment; // Higher priority
4545
const priority = dev ? -1 : builtIn ? 1 : 0;
4646
const grammars = extension.packageJSON.contributes?.grammars;
47-
if (grammars) {
47+
if (Array.isArray(grammars)) {
4848
for (const grammar of grammars) {
4949
const scopeName = grammar.scopeName;
5050
if (scopeName) {
@@ -64,7 +64,7 @@ function parseExtensions() {
6464
};
6565

6666
const injectTo = grammar.injectTo;
67-
if (injectTo) {
67+
if (Array.isArray(injectTo)) {
6868
for (const injectToScopeName of injectTo) {
6969
if (!grammarLanguages.scopeName[injectToScopeName]) {
7070
grammarLanguages.scopeName[injectToScopeName] = {
@@ -81,7 +81,7 @@ function parseExtensions() {
8181
}
8282

8383
const language = grammar.language;
84-
if (language) {
84+
if (typeof language === 'string') {
8585
if (!grammarLanguages.languageId[language] ||
8686
grammarLanguages.languageId[language].priority >= priority) { // VSCode picks the last extension
8787
grammarLanguages.languageId[language] = {

0 commit comments

Comments
 (0)