Skip to content

Commit d5a96fc

Browse files
committed
refactor file opening code
1 parent ad5bfae commit d5a96fc

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/dpldocs.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from "vscode";
22
import * as path from "path";
33
import { JSDOM } from "jsdom";
4-
import { reqText } from "./util";
4+
import { openTextDocumentAtRange, reqText } from "./util";
55
import { served } from "./extension";
66
import { DubDependencyInfo } from "./dub-view";
77
import { DOMParser } from "@xmldom/xmldom";
@@ -637,17 +637,8 @@ function focusModule(module_: string, line: number) {
637637
served.findFilesByModule(module_).then(files => {
638638
if (!files.length) {
639639
vscode.window.showErrorMessage("Could not find module " + module_);
640-
}
641-
else {
642-
vscode.workspace.openTextDocument(files[0]).then(doc => {
643-
vscode.window.showTextDocument(doc).then(editor => {
644-
if (line > 0) {
645-
var pos = new vscode.Position(line - 1, 0);
646-
editor.revealRange(new vscode.Range(pos, pos), vscode.TextEditorRevealType.InCenter);
647-
editor.selection.active = pos;
648-
}
649-
});
650-
});
640+
} else {
641+
openTextDocumentAtRange(vscode.Uri.parse(files[0]), line > 0 ? line - 1 : null);
651642
}
652643
});
653644
}

src/gcprofiler.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22
import { served } from "./extension";
3+
import { openTextDocumentAtRange } from "./util";
34

45
interface ProfileQuickPick extends vscode.QuickPickItem {
56
uri: string;
@@ -21,13 +22,7 @@ export class GCProfiler {
2122

2223
vscode.window.showQuickPick(items).then(item => {
2324
if (item)
24-
vscode.workspace.openTextDocument(vscode.Uri.parse(item.uri)).then(doc => {
25-
vscode.window.showTextDocument(doc).then(editor => {
26-
let line = doc.lineAt(item.line - 1);
27-
editor.revealRange(line.range, vscode.TextEditorRevealType.InCenterIfOutsideViewport);
28-
editor.selection = new vscode.Selection(line.range.start, line.range.start);
29-
});
30-
});
25+
openTextDocumentAtRange(vscode.Uri.parse(item.uri), item.line - 1);
3126
});
3227
}
3328

src/util.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,27 @@ export function showQuickPickWithInput<T>(items: T[] | Thenable<T[]>, options?:
148148
quickPick.show();
149149
});
150150
}
151+
152+
/**
153+
* @param uri The text document to open and show to the user.
154+
* @param lineOrRange
155+
* If null, only open the text document, don't scroll or select anything (default vscode behavior)
156+
* If a number, this is the 0-based line number to focus and put the cursor on.
157+
* If a range, this is a range to focus in the center of the editor and put the cursor at the start of.
158+
*/
159+
export function openTextDocumentAtRange(uri: vscode.Uri, lineOrRange: null | number | vscode.Position | vscode.Range): Thenable<vscode.TextEditor> {
160+
return vscode.workspace.openTextDocument(uri).then(doc =>
161+
vscode.window.showTextDocument(doc).then(editor => {
162+
if (lineOrRange !== null) {
163+
if (typeof lineOrRange == "number")
164+
lineOrRange = doc.lineAt(lineOrRange).range;
165+
if (lineOrRange instanceof vscode.Position)
166+
lineOrRange = new vscode.Range(lineOrRange, lineOrRange);
167+
168+
editor.revealRange(lineOrRange, vscode.TextEditorRevealType.InCenterIfOutsideViewport);
169+
editor.selection = new vscode.Selection(lineOrRange.start, lineOrRange.start);
170+
}
171+
return editor;
172+
})
173+
);
174+
}

0 commit comments

Comments
 (0)