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
Showing
12 changed files
with
168 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { URI } from 'vscode-uri'; | ||
|
||
import { toUrl } from './toUrl.mjs'; | ||
|
||
/** | ||
* Get the basename of a URL | ||
* @param url - the url to get the basename of | ||
* @returns the basename of the url | ||
*/ | ||
export function basename(url: string | URI | URL): string { | ||
const u = toUrl(url); | ||
return u.pathname.split('/').pop() || ''; | ||
} |
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,20 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { basename } from './basename.mjs'; | ||
|
||
describe('calcFileTypes', () => { | ||
test.each` | ||
url | expected | ||
${import.meta.url} | ${'basename.test.mts'} | ||
${'./file.js'} | ${'file.js'} | ||
${'./README.md'} | ${'README.md'} | ||
${'./dir/'} | ${''} | ||
${'./example.py'} | ${'example.py'} | ||
`('basename $url', ({ url, expected }) => { | ||
expect(basename(u(url))).toEqual(expected); | ||
}); | ||
}); | ||
|
||
function u(url: string | URL): string { | ||
return new URL(url, import.meta.url).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { getLanguageIdsForBaseFilename } from 'cspell-lib'; | ||
import type { URI } from 'vscode-uri'; | ||
|
||
import { basename } from './basename.mjs'; | ||
|
||
export function calcFileTypes(url: string | URI | URL): string[] { | ||
return getLanguageIdsForBaseFilename(basename(url)); | ||
} |
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,20 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { calcFileTypes } from './calcFileTypes.mjs'; | ||
|
||
describe('calcFileTypes', () => { | ||
test.each` | ||
url | expected | ||
${import.meta.url} | ${['typescript']} | ||
${'./file.js'} | ${['javascript']} | ||
${'./README.md'} | ${['markdown']} | ||
${'./dir/'} | ${[]} | ||
${'./example.py'} | ${['python']} | ||
`('calcFileTypes $url', ({ url, expected }) => { | ||
expect(calcFileTypes(u(url))).toEqual(expected); | ||
}); | ||
}); | ||
|
||
function u(url: string | URL): string { | ||
return new URL(url, import.meta.url).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { URI } from 'vscode-uri'; | ||
|
||
export function toUrl(url: URL | URI | string): URL { | ||
if (typeof url === 'string') { | ||
return new URL(url); | ||
} | ||
if (url instanceof URL) return url; | ||
return new URL(url.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { readTextDocument } from './readTextDocument.mjs'; | ||
export { isDir, isFile, readTextFile, stat } from './vfs.mjs'; |
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,11 @@ | ||
import { TextDocument } from 'vscode-languageserver-textdocument'; | ||
import type { URI } from 'vscode-uri'; | ||
|
||
import { calcFileTypes } from '../utils/calcFileTypes.mjs'; | ||
import { readTextFile } from './vfs.mjs'; | ||
|
||
export async function readTextDocument(url: string | URI | URL): Promise<TextDocument> { | ||
const content = await readTextFile(url); | ||
const fileTypes = calcFileTypes(url); | ||
return TextDocument.create(url.toString(), fileTypes[0] || 'plaintext', 0, content); | ||
} |
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,12 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { readTextDocument } from './readTextDocument.mjs'; | ||
|
||
describe('readTextDocument', () => { | ||
test('readTextDocument', async () => { | ||
const doc = await readTextDocument(import.meta.url); | ||
expect(doc.uri).toEqual(import.meta.url); | ||
expect(doc.getText()).toContain("describe('readTextDocument'"); | ||
expect(doc.languageId).toEqual('typescript'); | ||
}); | ||
}); |
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,48 @@ | ||
import type { VfsStat } from 'cspell-io'; | ||
import { getVirtualFS } from 'cspell-lib'; | ||
import type { URI } from 'vscode-uri'; | ||
|
||
import { toUrl } from '../utils/toUrl.mjs'; | ||
|
||
export async function stat(urlLike: URL | URI | string): Promise<VfsStat> { | ||
const url = toUrl(urlLike); | ||
const vfs = getVirtualFS(); | ||
const fs = vfs.getFS(url); | ||
const stat = await fs.stat(url); | ||
return stat; | ||
} | ||
|
||
export async function readTextFile(url: URL | URI | string): Promise<string> { | ||
const _url = toUrl(url); | ||
const vfs = getVirtualFS(); | ||
const fs = vfs.getFS(_url); | ||
const f = await fs.readFile(_url); | ||
return f.getText(); | ||
} | ||
|
||
export async function isFile(url: URL | URI | string): Promise<boolean> { | ||
try { | ||
const statInfo = await stat(url); | ||
return statInfo.isFile(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export async function isDir(url: URL | URI | string): Promise<boolean> { | ||
try { | ||
const statInfo = await stat(url); | ||
return statInfo.isDirectory(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export async function exists(url: URL | URI | string): Promise<boolean> { | ||
try { | ||
await stat(url); | ||
return true; | ||
} catch { | ||
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