Skip to content

Commit

Permalink
fix: Expose API to check documents from other extensions (#3327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored May 29, 2024
1 parent 468d176 commit 98003f2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/_integrationTests/src/extension.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const apiSignature: Api = {
cSpellClient: 'cSpellClient',
enableCurrentFileType: 'enableCurrentFileType',
disableCurrentFileType: 'disableCurrentFileType',
checkDocument: 'checkDocument',

// Legacy
enableLocal: 'enableLocal',
Expand Down
12 changes: 12 additions & 0 deletions packages/client/src/api.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type * as API from 'code-spell-checker-server/api';

import * as di from './di.mjs';

export type DocumentInfo = API.TextDocumentInfo;
export type CheckDocumentOptions = API.CheckDocumentOptions;
export type CheckDocumentResponse = API.CheckDocumentResult;

export async function checkDocument(doc: DocumentInfo, options?: CheckDocumentOptions): Promise<CheckDocumentResponse> {
const client = di.get('client');
return client.serverApi.checkDocument(doc, options);
}
2 changes: 2 additions & 0 deletions packages/client/src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Utils as UriUtils } from 'vscode-uri';

import { registerActionsMenu } from './actionMenu.mjs';
import * as addWords from './addWords.mjs';
import { checkDocument } from './api.mjs';
import { registerCspellInlineCompletionProviders } from './autocomplete.mjs';
import { CSpellClient } from './client/index.mjs';
import { registerSpellCheckerCodeActionProvider } from './codeAction.mjs';
Expand Down Expand Up @@ -222,6 +223,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionApi>
updateSettings: () => false,
cSpellClient: () => client,
getConfigurationForDocument,
checkDocument,

// Legacy
enableLocal: methods.enableLocale,
Expand Down
26 changes: 14 additions & 12 deletions packages/client/src/extensionApi.mts
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import type { Uri } from 'vscode';

import type { CheckDocumentOptions, CheckDocumentResponse, DocumentInfo } from './api.mjs';
import type { CSpellClient } from './client/index.mjs';
import type { ConfigTargetLegacy } from './settings/index.mjs';

export interface ExtensionApi {
registerConfig(path: string): void;
triggerGetSettings(): void;
enableLanguageId(languageId: string, uri?: string): Thenable<void>;
disableLanguageId(languageId: string, uri?: string): Thenable<void>;
enableCurrentFileType(): Thenable<void>;
disableCurrentFileType(): Thenable<void>;
addWordToUserDictionary(word: string): Thenable<void>;
addWordToWorkspaceDictionary(word: string, uri?: string | null | Uri): Thenable<void>;
enableLocale(target: ConfigTargetLegacy, locale: string): Thenable<void>;
disableLocale(target: ConfigTargetLegacy, locale: string): Thenable<void>;
enableLanguageId(languageId: string, uri?: string): Promise<void>;
disableLanguageId(languageId: string, uri?: string): Promise<void>;
enableCurrentFileType(): Promise<void>;
disableCurrentFileType(): Promise<void>;
addWordToUserDictionary(word: string): Promise<void>;
addWordToWorkspaceDictionary(word: string, uri?: string | null | Uri): Promise<void>;
enableLocale(target: ConfigTargetLegacy, locale: string): Promise<void>;
disableLocale(target: ConfigTargetLegacy, locale: string): Promise<void>;
updateSettings(): boolean;
cSpellClient(): CSpellClient;
checkDocument(doc: DocumentInfo, options?: CheckDocumentOptions): Promise<CheckDocumentResponse>;

/**
* @deprecated use {@link ExtensionApi.enableLocale}
*/
enableLocal(isGlobal: boolean, locale: string): Thenable<void>;
enableLocal(isGlobal: boolean, locale: string): Promise<void>;
/**
* @deprecated use {@link ExtensionApi.disableLocale}
*/
disableLocal(isGlobal: boolean, locale: string): Thenable<void>;
disableLocal(isGlobal: boolean, locale: string): Promise<void>;
/**
* @deprecated use {@link ExtensionApi.enableCurrentFileType}
*/
enableCurrentLanguage(): Thenable<void>;
enableCurrentLanguage(): Promise<void>;
/**
* @deprecated use {@link ExtensionApi.disableCurrentFileType}
*/
disableCurrentLanguage(): Thenable<void>;
disableCurrentLanguage(): Promise<void>;
}
5 changes: 2 additions & 3 deletions packages/client/src/repl/cmdCheck.mts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import type * as API from 'code-spell-checker-server/api';
import type { CancellationToken, Uri } from 'vscode';

import * as di from '../di.mjs';
import { checkDocument } from '../api.mjs';
import { colors } from './ansiUtils.mjs';
import { formatPath, relative } from './formatPath.mjs';

const maxPathLen = 60;

export async function cmdCheckDocument(uri: Uri | string, options: CheckDocumentsOptions, index?: number, count?: number): Promise<void> {
const client = di.get('client');
const { forceCheck, output, log, width } = options;

const startTs = performance.now();
const prefix = countPrefix(index, count);

output(`${prefix}${colors.gray(formatPath(relative(uri), Math.min(maxPathLen, width - 10 - prefix.length)))}`);
const result = await client.serverApi.checkDocument({ uri: uri.toString() }, { forceCheck });
const result = await checkDocument({ uri: uri.toString() }, { forceCheck });

const elapsed = performance.now() - startTs;
const elapsedTime = elapsed.toFixed(2) + 'ms';
Expand Down

0 comments on commit 98003f2

Please sign in to comment.