@@ -2,74 +2,65 @@ import * as vscode from "vscode";
2
2
import querystring from "querystring" ;
3
3
import { text } from "stream/consumers" ;
4
4
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
+
5
15
type TextDocument = {
6
16
uri : vscode . Uri ;
7
17
options : vscode . TextDocumentShowOptions ;
8
18
} ;
9
19
10
20
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 [ ] ;
14
25
}
15
26
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 )
25
30
) ;
26
- if ( ! workspaceFolder ) {
27
- return textDocuments ;
31
+ const fileUri = candidates . find ( isFileAtUri ) ;
32
+ if ( ! fileUri ) {
33
+ return [ ] ;
28
34
}
29
35
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,
36
45
options : {
37
- preserveFocus : false ,
38
- preview : false ,
39
- viewColumn : vscode . ViewColumn . Active ,
46
+ preserveFocus : ! firstDocument ,
47
+ preview : ! firstDocument ,
48
+ viewColumn,
40
49
} ,
41
50
} ) ;
42
- } catch ( error ) {
43
- console . error ( error ) ;
44
- return textDocuments ;
45
- }
51
+ } ;
46
52
47
53
// 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 ) ;
70
59
}
71
60
}
72
61
62
+ appendTextDocument ( fileUri ) ;
63
+
73
64
return textDocuments ;
74
65
}
75
66
0 commit comments