Skip to content

Commit 51bb8ec

Browse files
committed
Issue #113
1 parent b999daa commit 51bb8ec

8 files changed

+110
-49
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to the "QB64" extension will be documented in this file.
1616
- Issue [#110](https://github.com/QB64Official/vscode/issues/110) Add keypress to call the new stand alone color picker. (alt+p)
1717
- Issue [#108](https://github.com/QB64Official/vscode/issues/108) Include the help files (markdown) with the installer.
1818
- Bugs
19+
- Issue [#113](https://github.com/QB64Official/vscode/issues/113) Subs and Functions are not highlighted like there are in the classic IDE
1920
- Issue [#111](https://github.com/QB64Official/vscode/issues/111) Formatter: doesn't handle code with line numbers correctly.
2021
- Issue [#109](https://github.com/QB64Official/vscode/issues/109) Open in QB64 is not using the correct setting.
2122

releases/qb64-0.9.0.vsix

1.16 KB
Binary file not shown.

releases/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to the "QB64" extension will be documented in this file.
1616
- Issue [#110](https://github.com/QB64Official/vscode/issues/110) Add keypress to call the new stand alone color picker. (alt+p)
1717
- Issue [#108](https://github.com/QB64Official/vscode/issues/108) Include the help files (markdown) with the installer.
1818
- Bugs
19+
- Issue [#113](https://github.com/QB64Official/vscode/issues/113) Subs and Functions are not highlighted like there are in the classic IDE
1920
- Issue [#111](https://github.com/QB64Official/vscode/issues/111) Formatter: doesn't handle code with line numbers correctly.
2021
- Issue [#109](https://github.com/QB64Official/vscode/issues/109) Open in QB64 is not using the correct setting.
2122

src/commonFunctions.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { unescapeLeadingUnderscores } from "typescript";
21
import * as vscode from "vscode";
3-
import * as logFunctions from "./logFunctions"
42

53
/**
64
* Gets a new vscode.DocumentSelector
@@ -55,8 +53,9 @@ export function getAbsolutePath(base: string, relative: string): string {
5553

5654
/**
5755
* Creates a new range object from a regex match.
58-
* @param match Match with the start and stop for the range
59-
* @param lineNumber Line Number in the source file that range is for
56+
* @param match Match with the start and stop for the range.
57+
* @param lineNumber Line Number in the source file that range is for.
58+
* @param matchIndex The index in the match array to use.
6059
* @returns
6160
*/
6261
export function createRange(match: RegExpMatchArray, lineNumber: number, matchIndex: number = 0) {

src/decoratorFunctions.ts

+69-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22
import * as vscode from "vscode";
3-
import * as logFunctions from "./logFunctions"
4-
import * as commonFunctions from "./commonFunctions"
3+
import * as logFunctions from "./logFunctions";
4+
import * as commonFunctions from "./commonFunctions";
5+
import { symbolCache } from "./extension";
56

67
const red = 0;
78
const green = 1;
@@ -13,28 +14,35 @@ const decorationTypeCurrentRow = vscode.window.createTextEditorDecorationType(
1314
{
1415
fontWeight: "bold",
1516
borderRadius: "5px",
16-
dark: {
17-
border: "1px solid rgb(215,215,215); opacity: 0.5;",
18-
},
19-
light: {
20-
border: "1px solid rgb(115,115,115); opacity: 0.5;",
21-
}
17+
dark: { border: "1px solid rgb(215,215,215); opacity: 0.5;" },
18+
light: { border: "1px solid rgb(115,115,115); opacity: 0.5;" }
2219
}
2320
);
2421

25-
var lastLine: vscode.Position;
22+
const decorationTypeSub = vscode.window.createTextEditorDecorationType(
23+
{
24+
fontWeight: "bolder",
25+
dark: { color: "rgb(45, 156, 66);", },
26+
light: { color: "rgb(78, 74, 139);", }
27+
}
28+
);
2629

30+
let lastLine: vscode.Position;
2731
export function setupDecorate() {
28-
decorateAll(vscode.window.activeTextEditor); // Decorate on activate/first open
29-
30-
// Decorate when the text editor changes
31-
vscode.window.onDidChangeActiveTextEditor(async (e: { document: any; }) => {
32-
if (!e || !e.document) {
33-
return
32+
symbolCache.length = 0;
33+
vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', vscode.window.activeTextEditor.document.uri)
34+
.then(() => {
35+
decorateAll(vscode.window.activeTextEditor);
36+
});
37+
38+
vscode.window.onDidChangeActiveTextEditor((editor): void => {
39+
if (editor) {
40+
vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', editor.document.uri)
41+
.then(() => {
42+
decorateAll(editor);
43+
});
3444
}
35-
decorateAll(vscode.window.activeTextEditor);
3645
});
37-
3846
vscode.window.onDidChangeTextEditorSelection(() => { decorateSingleLine(vscode.window.activeTextEditor); })
3947
}
4048

@@ -55,12 +63,14 @@ function decorateSingleLine(editor: any) {
5563

5664
const config = vscode.workspace.getConfiguration("qb64");
5765

58-
let includeLeading: vscode.Range[] = []
59-
let includeTrailing: vscode.Range[] = []
60-
let todo: vscode.Range[] = []
66+
let includeLeading: vscode.Range[] = [];
67+
let includeTrailing: vscode.Range[] = [];
68+
let todo: vscode.Range[] = [];
69+
let subs: vscode.Range[] = [];
70+
6171

6272
if (lastLine && lastLine.line != currrentLine.line) {
63-
decorate(editor, lastLine.line, editor.document.lineAt(lastLine.line).text, outputChannnel, includeLeading, includeTrailing, todo);
73+
decorate(editor, lastLine.line, editor.document.lineAt(lastLine.line).text, outputChannnel, includeLeading, includeTrailing, todo, subs);
6474
}
6575

6676
if (config.get("isCurrentRowHighlightEnabled")) {
@@ -82,7 +92,7 @@ function decorateSingleLine(editor: any) {
8292
}
8393
}
8494

85-
function decorateAll(editor: any) {
95+
export function decorateAll(editor: any) {
8696

8797
if (!editor || editor.document.languageId == "Log" || editor.document.fileName == "extension-output-qb64-official.qb64-#3-QB64: Decorate") {
8898
return;
@@ -93,21 +103,24 @@ function decorateAll(editor: any) {
93103
const sourceCode = editor.document.getText().split('\n');
94104
let includeLeading: vscode.Range[] = [];
95105
let includeTrailing: vscode.Range[] = [];
96-
let todo: vscode.Range[] = [];
106+
let todos: vscode.Range[] = [];
107+
let subs: vscode.Range[] = [];
108+
97109
for (let line = 0; line < sourceCode.length; line++) {
98-
decorate(editor, line, sourceCode[line], outputChannnel, includeLeading, includeTrailing, todo);
110+
decorate(editor, line, sourceCode[line], outputChannnel, includeLeading, includeTrailing, todos, subs);
99111
}
100112

101113
editor.setDecorations(decorationTypeIncludeLeading, includeLeading);
102114
editor.setDecorations(decorationTypeIncludeTrailing, includeTrailing);
103-
editor.setDecorations(decorationTypeTodo, todo);
115+
editor.setDecorations(decorationTypeTodo, todos);
116+
editor.setDecorations(decorationTypeSub, subs);
104117

105118
} catch (error) {
106119
logFunctions.writeLine(`ERROR in decorateAll: ${error}`, outputChannnel);
107120
}
108121
}
109122

110-
function decorate(editor: any, lineNumber: number, lineOfCode: string, outputChannnel: any, includeLeading: vscode.Range[], includeTrailing: vscode.Range[], todo: vscode.Range[]) {
123+
function decorate(editor: any, lineNumber: number, lineOfCode: string, outputChannnel: any, includeLeading: vscode.Range[], includeTrailing: vscode.Range[], todos: vscode.Range[], subs: vscode.Range[]) {
111124

112125
if (!editor || editor.document.languageId == "Log" || editor.document.fileName == "extension-output-qb64-official.qb64-#3-QB64: Decorate") {
113126
return;
@@ -144,7 +157,36 @@ function decorate(editor: any, lineNumber: number, lineOfCode: string, outputCha
144157
if (config.get("isTodoHighlightEnabled")) {
145158
const matches = lineOfCode.matchAll(/(?<='*|rem*)TODO:|FIXIT:|FIXME:/ig);
146159
for (const match of matches) {
147-
todo.push(commonFunctions.createRange(match, lineNumber, 0));
160+
todos.push(commonFunctions.createRange(match, lineNumber, 0));
161+
}
162+
}
163+
164+
if (symbolCache && symbolCache.length > 0) {
165+
const tokens = lineOfCode.split(/[\s(]+/);
166+
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) {
167+
const sub = symbolCache.find((s) => s.name.toLowerCase() === tokens[tokenIndex].toLowerCase() && (s.kind === vscode.SymbolKind.Method || s.kind === vscode.SymbolKind.Function));
168+
if (sub) {
169+
170+
const matches = lineOfCode.matchAll(new RegExp(`\\s*(\\b(call|declare sub|sub|function|\\s+=\\s+)\\s+)?${commonFunctions.escapeRegExp(sub.name)}(?:\\()?(?!\\))`, 'gi'));
171+
172+
/*
173+
if (lineOfCode == "MachSpeed& = CalcDelay&") {
174+
console.log("here");
175+
}
176+
*/
177+
178+
for (let match of matches) {
179+
180+
let start: number = match.index < 1 ? match[0].toLowerCase().indexOf(sub.name.toLowerCase()) : match.index
181+
let stop: number = match.index < 1 ? start + sub.name.length : start + sub.name.length + 1
182+
183+
subs.push(
184+
new vscode.Range(
185+
new vscode.Position(lineNumber, start),
186+
new vscode.Position(lineNumber, stop)
187+
));
188+
}
189+
}
148190
}
149191
}
150192

src/extension.ts

+33-17
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import { HoverProvider } from "./providers/HoverProvider";
3535
// ]
3636
// }
3737

38-
39-
export function activate(context: vscode.ExtensionContext) {
38+
export var symbolCache: vscode.DocumentSymbol[] = [];
39+
export async function activate(context: vscode.ExtensionContext) {
4040
const config = vscode.workspace.getConfiguration("qb64")
4141
const documentSelector: vscode.DocumentSelector = commonFunctions.getDocumentSelector()
4242

@@ -52,48 +52,64 @@ export function activate(context: vscode.ExtensionContext) {
5252
}
5353
});
5454

55-
decoratorFunctions.setupDecorate();
56-
vscodeFucnctions.createFiles();
57-
gitFunctions.createGitignore();
55+
/*
56+
vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', vscode.window.activeTextEditor.document.uri)
57+
.then((symbols) => {
58+
symbolCache = symbols;
59+
// Handle the retrieved symbols here
60+
decoratorFunctions.setupDecorate();
61+
});
62+
*/
63+
64+
//decoratorFunctions.setupDecorate();
5865

5966
// Register Commands here
6067
webViewFunctions.setupAsciiChart(context);
61-
6268
context.subscriptions.push(vscode.commands.registerCommand('extension.showHelp', () => { showHelp(); }));
6369
context.subscriptions.push(vscode.commands.registerCommand('extension.openCompileLog', () => { openCompileLog(); }));
6470
context.subscriptions.push(vscode.commands.registerCommand('extension.showHelpIndexAlphabetical', () => { showHelpByName("Keyword-Reference---Alphabetical"); }));
6571
context.subscriptions.push(vscode.commands.registerCommand('extension.showHelpIndexUsage', () => { showHelpByName("Keyword-Reference---By-Usage"); }));
6672
context.subscriptions.push(vscode.commands.registerCommand('extension.runLint', () => { runLint(); }));
6773
context.subscriptions.push(vscode.commands.registerCommand('extension.openCurrentFileInQB64', () => { openCurrentFileInQB64(); }));
6874
context.subscriptions.push(vscode.commands.registerCommand('extension.addToGitIgnore', async (...selectedItems) => { addToGitIgnore(selectedItems); }));
69-
7075
context.subscriptions.push(vscode.commands.registerCommand('extension.removeLineNumbers', () => { removeLineNumbers(); }));
7176
context.subscriptions.push(vscode.commands.registerCommand('extension.renumberLines', () => { renumberLines(); }));
7277

7378
// Register Providers here
7479
context.subscriptions.push(vscode.languages.registerReferenceProvider(commonFunctions.getDocumentSelector(), new ReferenceProvider()));
7580
context.subscriptions.push(vscode.languages.registerDefinitionProvider(commonFunctions.getDocumentSelector(), new DefinitionProvider()));
7681
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(documentSelector, new DocumentSymbolProvider()));
77-
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(documentSelector, new DocumentFormattingEditProvider()));
7882
context.subscriptions.push(vscode.languages.registerHoverProvider(documentSelector, new HoverProvider()));
83+
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(documentSelector, new DocumentFormattingEditProvider()));
7984

8085
// Register Miscellaneous
8186
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory("QB64", new DebugAdapterDescriptorFactory()));
8287

88+
decoratorFunctions.setupDecorate();
89+
/*
90+
vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', vscode.window.activeTextEditor.document.uri)
91+
.then(() => {
92+
decoratorFunctions.decorateAll(vscode.window.activeTextEditor);
93+
});
94+
95+
vscode.window.onDidChangeActiveTextEditor((editor) => {
96+
if (editor) {
97+
vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', editor.document.uri)
98+
.then(() => {
99+
decoratorFunctions.decorateAll(editor);
100+
});
101+
}
102+
});
103+
*/
104+
105+
vscodeFucnctions.createFiles();
106+
gitFunctions.createGitignore();
107+
83108
let tempPath: string = config.get('helpPath')
84109
if (tempPath == null || tempPath.length < 1) {
85110
let tempPath = path.join(context.extensionPath, "help");
86111
config.update('helpPath', tempPath, vscode.ConfigurationTarget.Global);
87112
}
88-
89-
/*
90-
tempPath = config.get('compilerPath')
91-
if (tempPath == null || tempPath.length < 1) {
92-
tempPath = path.join(context.extensionPath, "binaries", "qb64-win.exe");
93-
config.update('compilerPath', tempPath, vscode.ConfigurationTarget.Global);
94-
}
95-
*/
96-
97113
}
98114

99115

src/providers/DocumentSymbolProvider.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
import * as vscode from "vscode";
3+
import { symbolCache } from "../extension";
34

45
// Setup the Outline window
56
export class DocumentSymbolProvider implements vscode.DocumentSymbolProvider {
@@ -118,6 +119,7 @@ export class DocumentSymbolProvider implements vscode.DocumentSymbolProvider {
118119
line.range,
119120
line.range
120121
);
122+
symbolCache.push(marker_symbol);
121123
if (symbolChildren) {
122124
marker_symbol.children = symbolChildren;
123125
symbolChildren = [];

0 commit comments

Comments
 (0)