-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Issue with spell checking SCM message (#2774)
- Loading branch information
Showing
6 changed files
with
154 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// import type { WorkspaceFolder } from 'vscode'; | ||
import { URI as Uri, Utils as UriUtils } from 'vscode-uri'; | ||
|
||
export const schemasWithSpecialHandling = { | ||
vscodeNoteBookCell: 'vscode-notebook-cell', | ||
vscodeScm: 'vscode-scm', | ||
} as const satisfies Readonly<Record<string, string>>; | ||
|
||
type SpecialHandlingFunction = (uri: Uri) => Uri; | ||
|
||
const _schemaMapToHandler = { | ||
[schemasWithSpecialHandling.vscodeNoteBookCell]: forceToFileUri, | ||
[schemasWithSpecialHandling.vscodeScm]: handleExtractUriFromQuery('rootUri', 'COMMIT_MSG.txt'), | ||
} as const satisfies Readonly<Record<string, SpecialHandlingFunction>>; | ||
|
||
const schemaMapToHandler: Readonly<Record<string, SpecialHandlingFunction>> = _schemaMapToHandler; | ||
|
||
const _alwaysIncludeMatchedFiles = [/^vscode-scm:/]; | ||
|
||
// handleSpecialUri | ||
export function handleSpecialUri(uri: Uri): Uri { | ||
if (!uriNeedsSpecialHandling(uri)) return uri; | ||
return getHandler(uri)(uri); | ||
} | ||
|
||
export function forceToFileUri(uri: Uri): Uri { | ||
if (uri.scheme === 'file') return uri; | ||
|
||
return uri.with({ | ||
scheme: 'file', | ||
query: '', | ||
fragment: '', | ||
}); | ||
} | ||
|
||
export function extractUriFromQueryParam(uri: Uri | string, param: string): Uri | undefined { | ||
const url = new URL(uri.toString(true)); | ||
const newUrl = decodeURIComponent(url.searchParams.get(param) || ''); | ||
if (!newUrl) return undefined; | ||
try { | ||
return Uri.parse(newUrl); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
export function uriNeedsSpecialHandling(uri: Uri): boolean { | ||
return uri.scheme in schemaMapToHandler; | ||
} | ||
|
||
export function isUrlIncludedByDefault(uri: Uri | string): boolean { | ||
const url = uri.toString(); | ||
|
||
for (const reg of _alwaysIncludeMatchedFiles) { | ||
if (reg.test(url)) return true; | ||
} | ||
return false; | ||
} | ||
|
||
function handleExtractUriFromQuery(param: string, appendFile?: string): (uri: Uri) => Uri { | ||
return (uri) => { | ||
const _uri = extractUriFromQueryParam(uri, param); | ||
if (_uri) { | ||
return appendFile ? UriUtils.joinPath(_uri, appendFile) : _uri; | ||
} | ||
return uri; | ||
}; | ||
} | ||
|
||
function getHandler(uri: Uri): SpecialHandlingFunction { | ||
return schemaMapToHandler[uri.scheme]; | ||
} |
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,39 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { URI as Uri } from 'vscode-uri'; | ||
|
||
import { extractUriFromQueryParam, forceToFileUri, handleSpecialUri } from './docUriHelper.mjs'; | ||
|
||
// cspell:ignore jsmith | ||
|
||
const sampleUrl1 = 'vscode-scm:git/scm0/input?rootUri=file:///c:/Users/jsmith/projects/cspell-dicts&name=Jonathan%20Smith&age=18'; | ||
const sampleUrl2 = 'vscode-scm:git/scm0/input?rootUri=file%3A///c%3A/Users/jsmith/projects/cspell-dicts&name=Jonathan%20Smith&age=18'; | ||
|
||
describe('', () => { | ||
test.each` | ||
uri | expected | ||
${import.meta.url} | ${Uri.parse(import.meta.url)} | ||
${new URL(import.meta.url).href} | ${Uri.parse(import.meta.url)} | ||
${sampleUrl1} | ${'file:///git/scm0/input'} | ||
`('forceToFileUri $uri', ({ uri, expected }) => { | ||
expect(forceToFileUri(Uri.parse(uri)).toString()).toEqual(expected.toString()); | ||
}); | ||
|
||
test.each` | ||
uri | expected | ||
${import.meta.url} | ${Uri.parse(import.meta.url)} | ||
${new URL(import.meta.url).href} | ${Uri.parse(import.meta.url)} | ||
${sampleUrl1} | ${'file:///c%3A/Users/jsmith/projects/cspell-dicts/COMMIT_MSG.txt'} | ||
`('handleSpecialUri $uri', ({ uri, expected }) => { | ||
expect(handleSpecialUri(Uri.parse(uri)).toString()).toEqual(expected.toString()); | ||
}); | ||
|
||
test.each` | ||
uri | param | expected | ||
${import.meta.url} | ${'rootUri'} | ${undefined} | ||
${sampleUrl1} | ${'rootUri'} | ${'file:///c%3A/Users/jsmith/projects/cspell-dicts'} | ||
${sampleUrl2} | ${'rootUri'} | ${'file:///c%3A/Users/jsmith/projects/cspell-dicts'} | ||
`('extractUriFromQueryParam $uri', ({ uri, param, expected }) => { | ||
const r = extractUriFromQueryParam(uri, param); | ||
expect(r?.toString()).toEqual(expected?.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