-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCSS fs provider. Fix microsoft/vscode#58204
- Loading branch information
Showing
13 changed files
with
247 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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 } from '../cssLanguageTypes'; | ||
import { TextDocument, DocumentLink } from '../cssLanguageService'; | ||
import * as nodes from '../parser/cssNodes'; | ||
|
||
export class SCSSNavigation extends CSSNavigation { | ||
constructor(private fileSystemProvider?: FileSystemProvider) { | ||
super(); | ||
} | ||
|
||
public async findDocumentLinks( | ||
document: TextDocument, | ||
stylesheet: nodes.Stylesheet, | ||
documentContext: DocumentContext | ||
): Promise<DocumentLink[]> { | ||
const links = await super.findDocumentLinks(document, stylesheet, documentContext); | ||
const fsProvider = this.fileSystemProvider; | ||
|
||
/** | ||
* Validate and correct links | ||
*/ | ||
for (let i = 0; i < links.length; i++) { | ||
if (links[i].target.endsWith('.scss') && !(await fileExists(links[i].target))) { | ||
const { originalBasename, normalizedBasename, normalizedUri, withBasename } = toNormalizedUri(links[i].target); | ||
// a.scss case | ||
if (originalBasename === normalizedBasename && await fileExists(withBasename('_' + originalBasename))) { | ||
links[i].target = withBasename('_' + originalBasename); | ||
continue; | ||
} | ||
// _a.scss case | ||
else if (originalBasename === '_' + normalizedBasename && await fileExists(normalizedUri)) { | ||
links[i].target = normalizedUri; | ||
continue; | ||
} | ||
|
||
// a/index.scss and a/_index.scss case | ||
const indexUri = withBasename(normalizedBasename.replace('.scss', '/index.scss')); | ||
const _indexUri = withBasename(normalizedBasename.replace('.scss', '/_index.scss')); | ||
|
||
if (await fileExists(indexUri)) { | ||
links[i].target = indexUri; | ||
} else if (await fileExists(_indexUri)) { | ||
links[i].target = _indexUri; | ||
} | ||
} | ||
} | ||
|
||
return links; | ||
|
||
function toNormalizedUri(uri: string) { | ||
const uriFragments = uri.split('/'); | ||
let normalizedBasename = uriFragments[uriFragments.length - 1]; | ||
if (normalizedBasename.startsWith('_')) { | ||
normalizedBasename = normalizedBasename.slice(1); | ||
} | ||
if (!normalizedBasename.endsWith('.scss')) { | ||
normalizedBasename += '.scss'; | ||
} | ||
|
||
const normalizedUri = [...uriFragments.slice(0, -1), normalizedBasename].join('/'); | ||
return { | ||
originalBasename: uriFragments[uriFragments.length - 1], | ||
normalizedUri, | ||
normalizedBasename, | ||
withBasename(newBaseName: string) { | ||
return [...uriFragments.slice(0, -1), newBaseName].join('/'); | ||
} | ||
}; | ||
} | ||
|
||
async function fileExists(uri: string) { | ||
if (!fsProvider) { | ||
return false; | ||
} | ||
|
||
try { | ||
const stat = await fsProvider.stat(uri); | ||
if (stat.type === FileType.Unknown && stat.size === -1) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters