Skip to content

Commit cd790e4

Browse files
Tyler Deemerty-d
authored andcommitted
Add document links to output window
1 parent 567fe1d commit cd790e4

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@
387387
"extensions": [
388388
"csp"
389389
]
390+
},
391+
{
392+
"id": "objectscript-output",
393+
"mimetypes": [
394+
"text/x-code-output"
395+
]
390396
}
391397
],
392398
"grammars": [

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import {
7878
} from "./utils";
7979
import { ObjectScriptDiagnosticProvider } from "./providers/ObjectScriptDiagnosticProvider";
8080
import { DocumentRangeFormattingEditProvider } from "./providers/DocumentRangeFormattingEditProvider";
81+
import { DocumentLinkProvider } from "./providers/DocumentLinkProvider";
8182

8283
/* proposed */
8384
import { FileSearchProvider } from "./providers/FileSystemProvider/FileSearchProvider";
@@ -874,6 +875,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
874875
documentSelector("objectscript-class"),
875876
new ObjectScriptClassCodeLensProvider()
876877
),
878+
vscode.languages.registerDocumentLinkProvider({ language: "objectscript-output" }, new DocumentLinkProvider()),
877879

878880
/* Anything we use from the VS Code proposed API */
879881
...proposed

src/providers/DocumentLinkProvider.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as vscode from "vscode";
2+
import { DocumentContentProvider } from "./DocumentContentProvider";
3+
import { ObjectScriptClassSymbolProvider } from "./ObjectScriptClassSymbolProvider";
4+
5+
interface StudioLink {
6+
uri: vscode.Uri;
7+
range: vscode.Range;
8+
filename: string;
9+
methodname?: string;
10+
offset: number;
11+
}
12+
13+
export class DocumentLinkProvider implements vscode.DocumentLinkProvider {
14+
public provideDocumentLinks(document: vscode.TextDocument): vscode.ProviderResult<StudioLink[]> {
15+
// Possible link formats:
16+
// SomePackage.SomeClass.cls(SomeMethod+offset)
17+
// SomePackage.SomeClass.cls(offset)
18+
// SomeRoutine.int(offset)
19+
const regexs = [/((?:\w|\.)+)\((\w+)\+(\d+)\)/, /((?:\w|\.)+)\((\d+)\)/];
20+
const documentLinks: StudioLink[] = [];
21+
22+
for (let i = 0; i < document.lineCount; i++) {
23+
const text = document.lineAt(i).text;
24+
25+
regexs.forEach((regex) => {
26+
const match = regex.exec(text);
27+
if (match != null) {
28+
const filename = match[1];
29+
let methodname;
30+
let offset;
31+
32+
if (match.length >= 4) {
33+
methodname = match[2];
34+
offset = parseInt(match[3]);
35+
} else {
36+
offset = parseInt(match[2]);
37+
}
38+
39+
documentLinks.push({
40+
range: new vscode.Range(
41+
new vscode.Position(i, match.index),
42+
new vscode.Position(i, match.index + match[0].length)
43+
),
44+
uri: DocumentContentProvider.getUri(filename),
45+
filename,
46+
methodname,
47+
offset,
48+
});
49+
}
50+
});
51+
}
52+
return documentLinks;
53+
}
54+
55+
public async resolveDocumentLink(link: StudioLink, token: vscode.CancellationToken): Promise<vscode.DocumentLink> {
56+
const editor = await vscode.window.showTextDocument(link.uri);
57+
let offset = link.offset;
58+
59+
// add the offset of the method if it is a class
60+
if (link.methodname) {
61+
console.log("running class code");
62+
const symbols = await new ObjectScriptClassSymbolProvider().provideDocumentSymbols(editor.document, token);
63+
const methods = symbols[0].children;
64+
const matchingMethod = methods.find(
65+
(method) => (method.detail === "ClassMethod" || method.detail === "Method ") && method.name === link.methodname
66+
);
67+
const methodLine = matchingMethod.range.start.line;
68+
offset += methodLine + 1;
69+
}
70+
71+
// move the cursor
72+
const cursor = editor.selection.active;
73+
const newPosition = cursor.with(offset, 0);
74+
editor.selection = new vscode.Selection(newPosition, newPosition);
75+
76+
return new vscode.DocumentLink(link.range, link.uri);
77+
}
78+
}

0 commit comments

Comments
 (0)