-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathscssNavigation.ts
144 lines (121 loc) · 4.08 KB
/
scssNavigation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { CSSNavigation } from './cssNavigation';
import { FileSystemProvider, DocumentContext, FileType, DocumentUri } from '../cssLanguageTypes';
import { TextDocument, DocumentLink } from '../cssLanguageService';
import * as nodes from '../parser/cssNodes';
import { URI } from 'vscode-uri';
export class SCSSNavigation extends CSSNavigation {
constructor(private fileSystemProvider?: FileSystemProvider) {
super();
}
protected isRawStringDocumentLinkNode(node: nodes.Node): boolean {
return (
super.isRawStringDocumentLinkNode(node) ||
node.type === nodes.NodeType.Use ||
node.type === nodes.NodeType.Forward
);
}
public async findDocumentLinks2(
document: TextDocument,
stylesheet: nodes.Stylesheet,
documentContext: DocumentContext
): Promise<DocumentLink[]> {
const links = this.findDocumentLinks(document, stylesheet, documentContext);
const fsProvider = this.fileSystemProvider;
const validLinks: DocumentLink[] = [];
/**
* Validate and correct links
*/
if (fsProvider) {
for (let i = 0; i < links.length; i++) {
const target = links[i].target;
if (!target) {
continue;
}
let parsedUri = null;
try {
parsedUri = URI.parse(target);
} catch (e) {
if (e instanceof URIError) {
continue;
}
throw e;
}
const pathVariations = toPathVariations(parsedUri);
if (!pathVariations) {
if (await fileExists(target)) {
validLinks.push(links[i]);
}
continue;
}
for (let j = 0; j < pathVariations.length; j++) {
if (await fileExists(pathVariations[j])) {
validLinks.push({
...links[i],
target: pathVariations[j]
});
break;
}
}
}
}
return validLinks;
function toPathVariations(uri: URI): DocumentUri[] | undefined {
// No valid path
if (uri.path === '') {
return undefined;
}
// No variation for links that ends with suffix
if (uri.path.endsWith('.scss') || uri.path.endsWith('.css')) {
return undefined;
}
// If a link is like a/, try resolving a/index.scss and a/_index.scss
if (uri.path.endsWith('/')) {
return [
uri.with({ path: uri.path + 'index.scss' }).toString(),
uri.with({ path: uri.path + '_index.scss' }).toString()
];
}
// Use `uri.path` since it's normalized to use `/` in all platforms
const pathFragments = uri.path.split('/');
const basename = pathFragments[pathFragments.length - 1];
const pathWithoutBasename = uri.path.slice(0, -basename.length);
// No variation for links such as _a
if (basename.startsWith('_')) {
if (uri.path.endsWith('.scss')) {
return undefined;
} else {
return [uri.with({ path: uri.path + '.scss' }).toString()];
}
}
const normalizedBasename = basename + '.scss';
const documentUriWithBasename = (newBasename: string) => {
return uri.with({ path: pathWithoutBasename + newBasename }).toString();
};
const normalizedPath = documentUriWithBasename(normalizedBasename);
const underScorePath = documentUriWithBasename('_' + normalizedBasename);
const indexPath = documentUriWithBasename(normalizedBasename.slice(0, -5) + '/index.scss');
const indexUnderscoreUri = documentUriWithBasename(normalizedBasename.slice(0, -5) + '/_index.scss');
const cssPath = documentUriWithBasename(normalizedBasename.slice(0, -5) + '.css');
return [normalizedPath, underScorePath, indexPath, indexUnderscoreUri, cssPath];
}
async function fileExists(documentUri: DocumentUri) {
if (!fsProvider) {
return false;
}
try {
const stat = await fsProvider.stat(documentUri);
if (stat.type === FileType.Unknown && stat.size === -1) {
return false;
}
return true;
} catch (err) {
return false;
}
}
}
}