-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09cd61c
commit c44706a
Showing
10 changed files
with
315 additions
and
68 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
94 changes: 94 additions & 0 deletions
94
packages/language-server/src/plugins/css/FileSystemProvider.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,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 { | ||
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)]); | ||
} | ||
} |
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.