Skip to content

Commit 8729450

Browse files
Merge pull request #609 from ty-d/DocumentLinksInOutput
Add Document Links in ObjectScript Output Window
2 parents cfef749 + 8710b55 commit 8729450

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-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";
@@ -847,6 +848,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
847848
documentSelector("objectscript-class"),
848849
new ObjectScriptClassCodeLensProvider()
849850
),
851+
vscode.languages.registerDocumentLinkProvider({ language: "objectscript-output" }, new DocumentLinkProvider()),
850852

851853
/* Anything we use from the VS Code proposed API */
852854
...proposed

src/providers/DocumentLinkProvider.ts

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

0 commit comments

Comments
 (0)