Skip to content

Commit

Permalink
Merge pull request #241 from jeanp413/fix-vscode-121586
Browse files Browse the repository at this point in the history
Fixes link resolution for filename with a suffix containing a dot
  • Loading branch information
aeschli authored May 28, 2021
2 parents 3121393 + fc554e6 commit 1fe4de0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
34 changes: 21 additions & 13 deletions src/services/cssNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {
Color, ColorInformation, ColorPresentation, DocumentHighlight, DocumentHighlightKind, DocumentLink, Location,
Position, Range, SymbolInformation, SymbolKind, TextEdit, WorkspaceEdit, TextDocument, DocumentContext, FileSystemProvider, DocumentUri, FileType
Position, Range, SymbolInformation, SymbolKind, TextEdit, WorkspaceEdit, TextDocument, DocumentContext, FileSystemProvider, FileType
} from '../cssLanguageTypes';
import * as nls from 'vscode-nls';
import * as nodes from '../parser/cssNodes';
Expand All @@ -17,6 +17,8 @@ import { dirname, joinPath } from '../utils/resources';

const localize = nls.loadMessageBundle();

type UnresolvedLinkData = { link: DocumentLink, isRawLink: boolean }

export class CSSNavigation {

constructor(protected fileSystemProvider: FileSystemProvider | undefined) {
Expand Down Expand Up @@ -96,26 +98,30 @@ export class CSSNavigation {
}

public findDocumentLinks(document: TextDocument, stylesheet: nodes.Stylesheet, documentContext: DocumentContext): DocumentLink[] {
const links = this.findUnresolvedLinks(document, stylesheet);
for (let i = 0; i < links.length; i++) {
const target = links[i].target;
const linkData = this.findUnresolvedLinks(document, stylesheet);
const resolvedLinks: DocumentLink[] = [];
for (let data of linkData) {
const link = data.link;
const target = link.target;
if (target && !(/^\w+:\/\//g.test(target))) {
const resolved = documentContext.resolveReference(target, document.uri);
if (resolved) {
links[i].target = resolved;
link.target = resolved;
}
}
resolvedLinks.push(link);
}
return links;
return resolvedLinks;
}

public async findDocumentLinks2(document: TextDocument, stylesheet: nodes.Stylesheet, documentContext: DocumentContext): Promise<DocumentLink[]> {
const links = this.findUnresolvedLinks(document, stylesheet);
const linkData = this.findUnresolvedLinks(document, stylesheet);
const resolvedLinks: DocumentLink[] = [];
for (let link of links) {
for (let data of linkData) {
const link = data.link;
const target = link.target;
if (target && !(/^\w+:\/\//g.test(target))) {
const resolvedTarget = await this.resolveRelativeReference(target, document.uri, documentContext);
const resolvedTarget = await this.resolveRelativeReference(target, document.uri, documentContext, data.isRawLink);
if (resolvedTarget !== undefined) {
link.target = resolvedTarget;
resolvedLinks.push(link);
Expand All @@ -128,8 +134,8 @@ export class CSSNavigation {
}


private findUnresolvedLinks(document: TextDocument, stylesheet: nodes.Stylesheet): DocumentLink[] {
const result: DocumentLink[] = [];
private findUnresolvedLinks(document: TextDocument, stylesheet: nodes.Stylesheet): UnresolvedLinkData[] {
const result: UnresolvedLinkData[] = [];

const collect = (uriStringNode: nodes.Node) => {
let rawUri = uriStringNode.getText();
Expand All @@ -142,7 +148,9 @@ export class CSSNavigation {
if (startsWith(rawUri, `'`) || startsWith(rawUri, `"`)) {
rawUri = rawUri.slice(1, -1);
}
result.push({ target: rawUri, range });

const isRawLink = uriStringNode.parent ? this.isRawStringDocumentLinkNode(uriStringNode.parent) : false;
result.push({ link: { target: rawUri, range }, isRawLink });
};

stylesheet.accept(candidate => {
Expand Down Expand Up @@ -275,7 +283,7 @@ export class CSSNavigation {
};
}

protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext): Promise<string | undefined> {
protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext, isRawLink?: boolean): Promise<string | undefined> {
// Following [css-loader](https://github.com/webpack-contrib/css-loader#url)
// and [sass-loader's](https://github.com/webpack-contrib/sass-loader#imports)
// convention, if an import path starts with ~ then use node module resolution
Expand Down
24 changes: 11 additions & 13 deletions src/services/scssNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@ export class SCSSNavigation extends CSSNavigation {
);
}

protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext): Promise<string | undefined> {
protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext, isRawLink?: boolean): Promise<string | undefined> {
if (startsWith(ref, 'sass:')) {
return undefined; // sass library
}
const target = await super.resolveRelativeReference(ref, documentUri, documentContext);
if (this.fileSystemProvider && target) {
const target = await super.resolveRelativeReference(ref, documentUri, documentContext, isRawLink);
if (this.fileSystemProvider && target && isRawLink) {
const parsedUri = URI.parse(target);
if (parsedUri.path) {
try {
const pathVariations = toPathVariations(parsedUri);
if (pathVariations) {
for (let j = 0; j < pathVariations.length; j++) {
if (await this.fileExists(pathVariations[j])) {
return pathVariations[j];
}
try {
const pathVariations = toPathVariations(parsedUri);
if (pathVariations) {
for (let j = 0; j < pathVariations.length; j++) {
if (await this.fileExists(pathVariations[j])) {
return pathVariations[j];
}
}
} catch (e) {
// ignore
}
} catch (e) {
// ignore

}
}
Expand Down

0 comments on commit 1fe4de0

Please sign in to comment.