-
-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat) css path completion #1533
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
abe1c38
path completion for css
jasonlyu123 e42cc7c
test
jasonlyu123 4df6a54
Merge branch 'master' of https://github.com/sveltejs/language-tools i…
jasonlyu123 0999b8b
naming
jasonlyu123 7720673
move FileSystemProvider and documentContext to css
jasonlyu123 005b8e4
oops camel case
jasonlyu123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { stat, readdir, Stats } from 'fs'; | ||
import { promisify } from 'util'; | ||
import { | ||
FileStat, | ||
FileSystemProvider as CSSFileSystemProvider, | ||
FileType | ||
} from 'vscode-css-languageservice'; | ||
import { urlToPath } from '../utils'; | ||
|
||
interface StatLike { | ||
isDirectory(): boolean; | ||
isFile(): boolean; | ||
isSymbolicLink(): boolean; | ||
} | ||
|
||
export class FileSystemProvider implements CSSFileSystemProvider { | ||
// TODO use fs/promises after we bumps the target nodejs versions | ||
private promisifyStat = promisify(stat); | ||
private promisifyReaddir = promisify(readdir); | ||
|
||
constructor() { | ||
this.readDirectory = this.readDirectory.bind(this); | ||
this.stat = this.stat.bind(this); | ||
} | ||
|
||
async stat(uri: string): Promise<FileStat> { | ||
const path = urlToPath(uri); | ||
|
||
if (!path) { | ||
return this.UnknownStat(); | ||
} | ||
|
||
let stat: Stats; | ||
try { | ||
stat = await this.promisifyStat(path); | ||
} catch (error) { | ||
if ( | ||
error != null && | ||
typeof error === 'object' && | ||
'code' in error && | ||
(error as { code: string }).code === 'ENOENT' | ||
) { | ||
return { | ||
type: FileType.Unknown, | ||
ctime: -1, | ||
mtime: -1, | ||
size: -1 | ||
}; | ||
} | ||
|
||
throw error; | ||
} | ||
|
||
return { | ||
ctime: stat.ctimeMs, | ||
mtime: stat.mtimeMs, | ||
size: stat.size, | ||
type: this.getFileType(stat) | ||
}; | ||
} | ||
|
||
private UnknownStat(): FileStat { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowercase first |
||
return { | ||
type: FileType.Unknown, | ||
ctime: -1, | ||
mtime: -1, | ||
size: -1 | ||
}; | ||
} | ||
|
||
private getFileType(stat: StatLike) { | ||
return stat.isDirectory() | ||
? FileType.Directory | ||
: stat.isFile() | ||
? FileType.File | ||
: stat.isSymbolicLink() | ||
? FileType.SymbolicLink | ||
: FileType.Unknown; | ||
} | ||
|
||
async readDirectory(uri: string): Promise<Array<[string, FileType]>> { | ||
const path = urlToPath(uri); | ||
|
||
if (!path) { | ||
return []; | ||
} | ||
|
||
const files = await this.promisifyReaddir(path, { | ||
withFileTypes: true | ||
}); | ||
|
||
return files.map((file) => [file.name, this.getFileType(file)]); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/language-server/src/lib/documents/documentContext.ts
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,42 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
// adopted from https://github.com/microsoft/vscode/blob/5ffcfde11d8b1b57634627f5094907789db09776/extensions/css-language-features/server/src/utils/documentContext.ts | ||
|
||
import { DocumentContext } from 'vscode-css-languageservice'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question like in |
||
import { WorkspaceFolder } from 'vscode-languageserver'; | ||
import { Utils, URI } from 'vscode-uri'; | ||
|
||
export function getDocumentContext( | ||
documentUri: string, | ||
workspaceFolders: WorkspaceFolder[] | ||
): DocumentContext { | ||
function getRootFolder(): string | undefined { | ||
for (const folder of workspaceFolders) { | ||
let folderURI = folder.uri; | ||
if (!folderURI.endsWith('/')) { | ||
folderURI = folderURI + '/'; | ||
} | ||
if (documentUri.startsWith(folderURI)) { | ||
return folderURI; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
return { | ||
resolveReference: (ref: string, base = documentUri) => { | ||
if (ref[0] === '/') { | ||
// resolve absolute path against the current workspace folder | ||
const folderUri = getRootFolder(); | ||
if (folderUri) { | ||
return folderUri + ref.substr(1); | ||
} | ||
} | ||
base = base.substr(0, base.lastIndexOf('/') + 1); | ||
return Utils.resolvePath(URI.parse(base), ref).toString(); | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You added this to lib and it's only used inside
plugins/css
. What's the reason for this? Do you think this could be useful outside the CSS plugin, too, or that it's not directly connected to the CSS plugin?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vscode-html-languageservice
has the same API for path completion. Originally I was thinking maybe we can add it too. But I am not sure is useful inside a svelte file. Since most of the time, the relative path to an asset does work like plain HTML. I think I'll move it toplugins/css
for now. We could move it out if we want to use it in HTML later.