mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-22 01:29:04 +01:00
Adding the server and client code to the JSON language features extensions to enable JSONC sorting in VS Code (#174352)
* temporarily changing the funcion onFromat to be able to trigger the sorting on real examples * adding the DocumentSortingRequest * accesssing directly the text edits from the request * added code in order to provide the link between the json sorting capabiliites and vscode * adapting to the new api output of the sort function * instead of using await use then inside of the command that registers * textEditor replaces window.activeTextEditor * adding changes from review * update service * use SortOptions --------- Co-authored-by: Martin Aeschlimann <martinae@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
b4e6254d60
commit
eee87af252
@@ -11,7 +11,7 @@ import {
|
||||
ProviderResult, TextEdit, Range, Position, Disposable, CompletionItem, CompletionList, CompletionContext, Hover, MarkdownString, FoldingContext, DocumentSymbol, SymbolInformation, l10n
|
||||
} from 'vscode';
|
||||
import {
|
||||
LanguageClientOptions, RequestType, NotificationType,
|
||||
LanguageClientOptions, RequestType, NotificationType, FormattingOptions as LSPFormattingOptions,
|
||||
DidChangeConfigurationNotification, HandleDiagnosticsSignature, ResponseError, DocumentRangeFormattingParams,
|
||||
DocumentRangeFormattingRequest, ProvideCompletionItemsSignature, ProvideHoverSignature, BaseLanguageClient, ProvideFoldingRangeSignature, ProvideDocumentSymbolsSignature, ProvideDocumentColorsSignature
|
||||
} from 'vscode-languageclient';
|
||||
@@ -36,6 +36,23 @@ namespace LanguageStatusRequest {
|
||||
export const type: RequestType<string, JSONLanguageStatus, any> = new RequestType('json/languageStatus');
|
||||
}
|
||||
|
||||
interface SortOptions extends LSPFormattingOptions {
|
||||
}
|
||||
|
||||
interface DocumentSortingParams {
|
||||
/**
|
||||
* The uri of the document to sort.
|
||||
*/
|
||||
readonly uri: string;
|
||||
/**
|
||||
* The format options
|
||||
*/
|
||||
readonly options: SortOptions;
|
||||
}
|
||||
|
||||
namespace DocumentSortingRequest {
|
||||
export const type: RequestType<DocumentSortingParams, TextEdit[], any> = new RequestType('json/sort');
|
||||
}
|
||||
|
||||
export interface ISchemaAssociations {
|
||||
[pattern: string]: string[];
|
||||
@@ -146,6 +163,37 @@ export async function startClient(context: ExtensionContext, newLanguageClient:
|
||||
window.showInformationMessage(l10n.t('JSON schema cache cleared.'));
|
||||
}));
|
||||
|
||||
toDispose.push(commands.registerCommand('json.sort', async () => {
|
||||
|
||||
if (isClientReady) {
|
||||
const textEditor = window.activeTextEditor;
|
||||
if (textEditor) {
|
||||
const document = textEditor.document;
|
||||
const filesConfig = workspace.getConfiguration('files', document);
|
||||
const options: SortOptions = {
|
||||
tabSize: textEditor.options.tabSize ? Number(textEditor.options.tabSize) : 4,
|
||||
insertSpaces: textEditor.options.insertSpaces ? Boolean(textEditor.options.insertSpaces) : true,
|
||||
trimTrailingWhitespace: filesConfig.get<boolean>('trimTrailingWhitespace'),
|
||||
trimFinalNewlines: filesConfig.get<boolean>('trimFinalNewlines'),
|
||||
insertFinalNewline: filesConfig.get<boolean>('insertFinalNewline'),
|
||||
};
|
||||
const params: DocumentSortingParams = {
|
||||
uri: document.uri.toString(),
|
||||
options
|
||||
};
|
||||
const textEdits = await client.sendRequest(DocumentSortingRequest.type, params);
|
||||
const success = await textEditor.edit(mutator => {
|
||||
for (const edit of textEdits) {
|
||||
mutator.replace(client.protocol2CodeConverter.asRange(edit.range), edit.newText);
|
||||
}
|
||||
});
|
||||
if (!success) {
|
||||
window.showErrorMessage(l10n.t('Failed to sort the JSONC document, please consider opening an issue.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// Options to control the language client
|
||||
const clientOptions: LanguageClientOptions = {
|
||||
// Register the server for json documents
|
||||
|
||||
Reference in New Issue
Block a user