mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-23 11:49:38 +00:00
single AutoInsertRequest message for both autoQuote and autoClose
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
import {
|
||||
Connection, TextDocuments, InitializeParams, InitializeResult, RequestType,
|
||||
DocumentRangeFormattingRequest, Disposable, TextDocumentPositionParams, ServerCapabilities,
|
||||
DocumentRangeFormattingRequest, Disposable, ServerCapabilities,
|
||||
ConfigurationRequest, ConfigurationParams, DidChangeWorkspaceFoldersNotification,
|
||||
DocumentColorRequest, ColorPresentationRequest, TextDocumentSyncKind, NotificationType, RequestType0, DocumentFormattingRequest, FormattingOptions, TextEdit
|
||||
} from 'vscode-languageserver';
|
||||
@@ -34,12 +34,23 @@ namespace CustomDataContent {
|
||||
export const type: RequestType<string, string, any> = new RequestType('html/customDataContent');
|
||||
}
|
||||
|
||||
namespace QuoteCreateRequest {
|
||||
export const type: RequestType<TextDocumentPositionParams, string, any> = new RequestType('html/quote');
|
||||
interface AutoInsertParams {
|
||||
/**
|
||||
* The auto insert kind
|
||||
*/
|
||||
kind: 'autoQuote' | 'autoClose';
|
||||
/**
|
||||
* The text document.
|
||||
*/
|
||||
textDocument: TextDocumentIdentifier;
|
||||
/**
|
||||
* The position inside the text document.
|
||||
*/
|
||||
position: Position;
|
||||
}
|
||||
|
||||
namespace TagCloseRequest {
|
||||
export const type: RequestType<TextDocumentPositionParams, string | null, any> = new RequestType('html/tag');
|
||||
namespace AutoInsertRequest {
|
||||
export const type: RequestType<AutoInsertParams, string, any> = new RequestType('html/autoInsert');
|
||||
}
|
||||
|
||||
// experimental: semantic tokens
|
||||
@@ -487,36 +498,20 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
|
||||
}, [], `Error while computing color presentations for ${params.textDocument.uri}`, token);
|
||||
});
|
||||
|
||||
connection.onRequest(QuoteCreateRequest.type, (params, token) => {
|
||||
connection.onRequest(AutoInsertRequest.type, (params, token) => {
|
||||
return runSafe(runtime, async () => {
|
||||
const document = documents.get(params.textDocument.uri);
|
||||
if (document) {
|
||||
const pos = params.position;
|
||||
if (pos.character > 0) {
|
||||
const mode = languageModes.getModeAtPosition(document, Position.create(pos.line, pos.character - 1));
|
||||
if (mode && mode.doAutoQuote) {
|
||||
return mode.doAutoQuote(document, pos);
|
||||
if (mode && mode.doAutoInsert) {
|
||||
return mode.doAutoInsert(document, pos, params.kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}, null, `Error while computing tag close actions for ${params.textDocument.uri}`, token);
|
||||
});
|
||||
|
||||
connection.onRequest(TagCloseRequest.type, (params, token) => {
|
||||
return runSafe(runtime, async () => {
|
||||
const document = documents.get(params.textDocument.uri);
|
||||
if (document) {
|
||||
const pos = params.position;
|
||||
if (pos.character > 0) {
|
||||
const mode = languageModes.getModeAtPosition(document, Position.create(pos.line, pos.character - 1));
|
||||
if (mode && mode.doAutoClose) {
|
||||
return mode.doAutoClose(document, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}, null, `Error while computing tag close actions for ${params.textDocument.uri}`, token);
|
||||
}, null, `Error while computing auto insert actions for ${params.textDocument.uri}`, token);
|
||||
});
|
||||
|
||||
connection.onFoldingRanges((params, token) => {
|
||||
|
||||
@@ -55,23 +55,21 @@ export function getHTMLMode(htmlLanguageService: HTMLLanguageService, workspace:
|
||||
async getFoldingRanges(document: TextDocument): Promise<FoldingRange[]> {
|
||||
return htmlLanguageService.getFoldingRanges(document);
|
||||
},
|
||||
async doAutoQuote(document: TextDocument, position: Position, settings = workspace.settings) {
|
||||
const htmlSettings = settings?.html;
|
||||
const options = merge(htmlSettings?.suggest, {});
|
||||
options.attributeDefaultValue = htmlSettings?.completion?.attributeDefaultValue ?? 'doublequotes';
|
||||
async doAutoInsert(document: TextDocument, position: Position, kind: 'autoQuote' | 'autoClose', settings = workspace.settings) {
|
||||
const offset = document.offsetAt(position);
|
||||
const text = document.getText();
|
||||
if (kind === 'autoQuote') {
|
||||
if (offset > 0 && text.charAt(offset - 1) === '=') {
|
||||
const htmlSettings = settings?.html;
|
||||
const options = merge(htmlSettings?.suggest, {});
|
||||
options.attributeDefaultValue = htmlSettings?.completion?.attributeDefaultValue ?? 'doublequotes';
|
||||
|
||||
const offset = document.offsetAt(position);
|
||||
const text = document.getText();
|
||||
if (offset > 0 && text.charAt(offset - 1) === '=') {
|
||||
return htmlLanguageService.doQuoteComplete(document, position, htmlDocuments.get(document), options);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
async doAutoClose(document: TextDocument, position: Position) {
|
||||
const offset = document.offsetAt(position);
|
||||
const text = document.getText();
|
||||
if (offset > 0 && text.charAt(offset - 1).match(/[>\/]/g)) {
|
||||
return htmlLanguageService.doTagComplete(document, position, htmlDocuments.get(document));
|
||||
return htmlLanguageService.doQuoteComplete(document, position, htmlDocuments.get(document), options);
|
||||
}
|
||||
} else if (kind === 'autoClose') {
|
||||
if (offset > 0 && text.charAt(offset - 1).match(/[>\/]/g)) {
|
||||
return htmlLanguageService.doTagComplete(document, position, htmlDocuments.get(document));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
@@ -72,8 +72,7 @@ export interface LanguageMode {
|
||||
format?: (document: TextDocument, range: Range, options: FormattingOptions, settings?: Settings) => Promise<TextEdit[]>;
|
||||
findDocumentColors?: (document: TextDocument) => Promise<ColorInformation[]>;
|
||||
getColorPresentations?: (document: TextDocument, color: Color, range: Range) => Promise<ColorPresentation[]>;
|
||||
doAutoQuote?: (document: TextDocument, position: Position) => Promise<string | null>;
|
||||
doAutoClose?: (document: TextDocument, position: Position) => Promise<string | null>;
|
||||
doAutoInsert?: (document: TextDocument, position: Position, kind: 'autoClose' | 'autoQuote') => Promise<string | null>;
|
||||
findMatchingTagPosition?: (document: TextDocument, position: Position) => Promise<Position | null>;
|
||||
getFoldingRanges?: (document: TextDocument) => Promise<FoldingRange[]>;
|
||||
onDocumentRemoved(document: TextDocument): void;
|
||||
|
||||
Reference in New Issue
Block a user