Skip to content

Commit 95f18c0

Browse files
committed
Handle view component files
1 parent 77665f9 commit 95f18c0

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

src/extension.ts

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,65 @@ import * as vscode from "vscode";
22
import querystring from "querystring";
33
import { text } from "stream/consumers";
44

5+
async function isFileAtUri(uri: vscode.Uri): Promise<boolean> {
6+
try {
7+
return (
8+
((await vscode.workspace.fs.stat(uri)).type & vscode.FileType.File) !== 0
9+
);
10+
} catch {
11+
return false;
12+
}
13+
}
14+
515
type TextDocument = {
616
uri: vscode.Uri;
717
options: vscode.TextDocumentShowOptions;
818
};
919

1020
async function editorTextDocuments(path: string): Promise<TextDocument[]> {
11-
const textDocuments: TextDocument[] = [];
12-
if (!vscode.workspace.workspaceFolders) {
13-
return textDocuments;
21+
// Without any workspace folders we will not be able to open the requested path
22+
const workspaceFolders = vscode.workspace.workspaceFolders;
23+
if (!workspaceFolders) {
24+
return [];
1425
}
1526

16-
const workspaceFolder = vscode.workspace.workspaceFolders.find(
17-
async ({ uri }) => {
18-
const workspaceFileUri = vscode.Uri.joinPath(uri, path);
19-
try {
20-
await vscode.workspace.fs.stat(workspaceFileUri);
21-
} catch (error) {
22-
return false;
23-
}
24-
}
27+
// See if any workspace folder contains a file at the provided path
28+
const candidates = workspaceFolders.map(({ uri }) =>
29+
vscode.Uri.joinPath(uri, path)
2530
);
26-
if (!workspaceFolder) {
27-
return textDocuments;
31+
const fileUri = candidates.find(isFileAtUri);
32+
if (!fileUri) {
33+
return [];
2834
}
2935

30-
const workspaceFileUri = vscode.Uri.joinPath(workspaceFolder.uri, path);
31-
try {
32-
await vscode.workspace.fs.stat(workspaceFileUri);
33-
34-
textDocuments.unshift({
35-
uri: workspaceFileUri,
36+
const textDocuments: TextDocument[] = [];
37+
const appendTextDocument = (uri: vscode.Uri) => {
38+
const firstDocument = textDocuments.length === 0;
39+
const viewColumn = firstDocument
40+
? vscode.ViewColumn.Active
41+
: vscode.ViewColumn.Beside;
42+
43+
textDocuments.push({
44+
uri,
3645
options: {
37-
preserveFocus: false,
38-
preview: false,
39-
viewColumn: vscode.ViewColumn.Active,
46+
preserveFocus: !firstDocument,
47+
preview: !firstDocument,
48+
viewColumn,
4049
},
4150
});
42-
} catch (error) {
43-
console.error(error);
44-
return textDocuments;
45-
}
51+
};
4652

4753
// If the filepath is a view component, open a split view with the HTML file as the first editor
48-
const viewComponentHtmlPath = path.replace(".rb", ".html.erb");
49-
if (path !== viewComponentHtmlPath) {
50-
const componentHtmlUri = vscode.Uri.joinPath(
51-
workspaceFolder.uri,
52-
path.replace(".rb", ".html.erb")
53-
);
54-
55-
try {
56-
await vscode.workspace.fs.stat(componentHtmlUri);
57-
textDocuments.unshift({
58-
uri: componentHtmlUri,
59-
options: {
60-
preserveFocus: true,
61-
62-
// If the user doesn't start editing this file, close it automatically
63-
preview: true,
64-
viewColumn: vscode.ViewColumn.Beside,
65-
},
66-
});
67-
} catch (error) {
68-
console.error(error);
69-
return textDocuments;
54+
const viewComponentHtmlPath = fileUri.path.replace(".rb", ".html.erb");
55+
if (fileUri.path !== viewComponentHtmlPath) {
56+
const componentHtmlUri = vscode.Uri.file(viewComponentHtmlPath);
57+
if (await isFileAtUri(componentHtmlUri)) {
58+
appendTextDocument(componentHtmlUri);
7059
}
7160
}
7261

62+
appendTextDocument(fileUri);
63+
7364
return textDocuments;
7465
}
7566

0 commit comments

Comments
 (0)