mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 13:00:58 +00:00
Merge pull request #191809 from microsoft/aiday/codeActionInsteadOfSetting
Change to code action from setting
This commit is contained in:
commit
0868714c95
|
@ -8,7 +8,7 @@ export type JSONLanguageStatus = { schemas: string[] };
|
|||
import {
|
||||
workspace, window, languages, commands, ExtensionContext, extensions, Uri, ColorInformation,
|
||||
Diagnostic, StatusBarAlignment, TextEditor, TextDocument, FormattingOptions, CancellationToken, FoldingRange,
|
||||
ProviderResult, TextEdit, Range, Position, Disposable, CompletionItem, CompletionList, CompletionContext, Hover, MarkdownString, FoldingContext, DocumentSymbol, SymbolInformation, l10n, TextEditorOptions
|
||||
ProviderResult, TextEdit, Range, Position, Disposable, CompletionItem, CompletionList, CompletionContext, Hover, MarkdownString, FoldingContext, DocumentSymbol, SymbolInformation, l10n
|
||||
} from 'vscode';
|
||||
import {
|
||||
LanguageClientOptions, RequestType, NotificationType, FormattingOptions as LSPFormattingOptions,
|
||||
|
@ -102,7 +102,6 @@ export type JSONSchemaSettings = {
|
|||
export namespace SettingIds {
|
||||
export const enableFormatter = 'json.format.enable';
|
||||
export const enableKeepLines = 'json.format.keepLines';
|
||||
export const enableSortOnSave = 'json.sortOnSave.enable';
|
||||
export const enableValidation = 'json.validate.enable';
|
||||
export const enableSchemaDownload = 'json.schemaDownload.enable';
|
||||
export const maxItemsComputed = 'json.maxItemsComputed';
|
||||
|
@ -171,15 +170,6 @@ export async function startClient(context: ExtensionContext, newLanguageClient:
|
|||
window.showInformationMessage(l10n.t('JSON schema cache cleared.'));
|
||||
}));
|
||||
|
||||
toDispose.push(workspace.onWillSaveTextDocument(event => {
|
||||
const sortOnSave = workspace.getConfiguration().get<boolean>(SettingIds.enableSortOnSave);
|
||||
const document = event.document;
|
||||
if (sortOnSave && (document.languageId === 'json' || document.languageId === 'jsonc')) {
|
||||
const documentOptions = getOptionsForDocument(document);
|
||||
const textEditsPromise = getSortTextEdits(document, documentOptions?.tabSize, documentOptions?.insertSpaces);
|
||||
event.waitUntil(textEditsPromise);
|
||||
}
|
||||
}));
|
||||
|
||||
toDispose.push(commands.registerCommand('json.sort', async () => {
|
||||
|
||||
|
@ -643,12 +633,3 @@ function updateMarkdownString(h: MarkdownString): MarkdownString {
|
|||
function isSchemaResolveError(d: Diagnostic) {
|
||||
return d.code === /* SchemaResolveError */ 0x300;
|
||||
}
|
||||
|
||||
function getOptionsForDocument(document: TextDocument): TextEditorOptions | undefined {
|
||||
for (const editor of window.visibleTextEditors) {
|
||||
if (editor.document.uri.toString() === document.uri.toString()) {
|
||||
return editor.options;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -91,12 +91,6 @@
|
|||
"default": false,
|
||||
"description": "%json.format.keepLines.desc%"
|
||||
},
|
||||
"json.sortOnSave.enable": {
|
||||
"type": "boolean",
|
||||
"scope": "window",
|
||||
"default": false,
|
||||
"description": "%json.sortOnSave.enable.desc%"
|
||||
},
|
||||
"json.trace.server": {
|
||||
"type": "string",
|
||||
"scope": "window",
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
"json.schemas.schema.desc": "The schema definition for the given URL. The schema only needs to be provided to avoid accesses to the schema URL.",
|
||||
"json.format.enable.desc": "Enable/disable default JSON formatter",
|
||||
"json.format.keepLines.desc" : "Keep all existing new lines when formatting.",
|
||||
"json.sortOnSave.enable.desc": "Enable/disable default sorting on save",
|
||||
"json.validate.enable.desc": "Enable/disable JSON validation.",
|
||||
"json.tracing.desc": "Traces the communication between VS Code and the JSON language server.",
|
||||
"json.colorDecorators.enable.desc": "Enables or disables color decorators",
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
import {
|
||||
Connection,
|
||||
TextDocuments, InitializeParams, InitializeResult, NotificationType, RequestType,
|
||||
DocumentRangeFormattingRequest, Disposable, ServerCapabilities, TextDocumentSyncKind, TextEdit, DocumentFormattingRequest, TextDocumentIdentifier, FormattingOptions, Diagnostic
|
||||
DocumentRangeFormattingRequest, Disposable, ServerCapabilities, TextDocumentSyncKind, TextEdit, DocumentFormattingRequest, TextDocumentIdentifier, FormattingOptions, Diagnostic, CodeAction, CodeActionKind
|
||||
} from 'vscode-languageserver';
|
||||
|
||||
import { runSafe, runSafeAsync } from './utils/runner';
|
||||
|
@ -14,6 +14,7 @@ import { DiagnosticsSupport, registerDiagnosticsPullSupport, registerDiagnostics
|
|||
import { TextDocument, JSONDocument, JSONSchema, getLanguageService, DocumentLanguageSettings, SchemaConfiguration, ClientCapabilities, Range, Position, SortOptions } from 'vscode-json-languageservice';
|
||||
import { getLanguageModelCache } from './languageModelCache';
|
||||
import { Utils, URI } from 'vscode-uri';
|
||||
import * as l10n from '@vscode/l10n';
|
||||
|
||||
type ISchemaAssociations = Record<string, string[]>;
|
||||
|
||||
|
@ -188,7 +189,8 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
|
|||
documentSelector: null,
|
||||
interFileDependencies: false,
|
||||
workspaceDiagnostics: false
|
||||
}
|
||||
},
|
||||
codeActionProvider: true
|
||||
};
|
||||
|
||||
return { capabilities };
|
||||
|
@ -424,6 +426,21 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
|
|||
}, [], `Error while computing document symbols for ${documentSymbolParams.textDocument.uri}`, token);
|
||||
});
|
||||
|
||||
connection.onCodeAction((codeActionParams, token) => {
|
||||
return runSafeAsync(runtime, async () => {
|
||||
const document = documents.get(codeActionParams.textDocument.uri);
|
||||
if (document) {
|
||||
const sortCodeAction = CodeAction.create('Sort JSON', CodeActionKind.Source.concat('.sort', '.json'));
|
||||
sortCodeAction.command = {
|
||||
command: 'json.sort',
|
||||
title: l10n.t('Sort JSON')
|
||||
};
|
||||
return [sortCodeAction];
|
||||
}
|
||||
return [];
|
||||
}, [], `Error while computing code actions for ${codeActionParams.textDocument.uri}`, token);
|
||||
});
|
||||
|
||||
function onFormat(textDocument: TextDocumentIdentifier, range: Range | undefined, options: FormattingOptions): TextEdit[] {
|
||||
|
||||
options.keepLines = keepLinesEnabled;
|
||||
|
|
Loading…
Reference in a new issue