diff --git a/packages/plugin-ext/src/common/plugin-api-rpc.ts b/packages/plugin-ext/src/common/plugin-api-rpc.ts index c0ce507624058..6c7dcb776e78c 100644 --- a/packages/plugin-ext/src/common/plugin-api-rpc.ts +++ b/packages/plugin-ext/src/common/plugin-api-rpc.ts @@ -1436,12 +1436,19 @@ export interface SerializedEnterAction { removeText?: number; } +export interface SerializedAutoClosingPair { + open: string; + close: string; + notIn?: string[]; +} + export interface SerializedLanguageConfiguration { comments?: CommentRule; brackets?: CharacterPair[]; wordPattern?: SerializedRegExp; indentationRules?: SerializedIndentationRule; onEnterRules?: SerializedOnEnterRule[]; + autoClosingPairs?: SerializedAutoClosingPair[] } export interface CodeActionDto { diff --git a/packages/plugin-ext/src/main/browser/languages-main.ts b/packages/plugin-ext/src/main/browser/languages-main.ts index 9d128eda986ac..6b6dcbbe6d5fb 100644 --- a/packages/plugin-ext/src/main/browser/languages-main.ts +++ b/packages/plugin-ext/src/main/browser/languages-main.ts @@ -167,6 +167,7 @@ export class LanguagesMainImpl implements LanguagesMain, Disposable { wordPattern: reviveRegExp(configuration.wordPattern), indentationRules: reviveIndentationRule(configuration.indentationRules), onEnterRules: reviveOnEnterRules(configuration.onEnterRules), + autoClosingPairs: configuration.autoClosingPairs }; this.register(handle, monaco.languages.setLanguageConfiguration(languageId, config)); diff --git a/packages/plugin-ext/src/plugin/languages-utils.ts b/packages/plugin-ext/src/plugin/languages-utils.ts index d52f922f42ed8..96d033d605b8b 100644 --- a/packages/plugin-ext/src/plugin/languages-utils.ts +++ b/packages/plugin-ext/src/plugin/languages-utils.ts @@ -15,7 +15,8 @@ // ***************************************************************************** import * as theia from '@theia/plugin'; -import { SerializedIndentationRule, SerializedOnEnterRule, SerializedRegExp } from '../common'; +import { SerializedAutoClosingPair, SerializedIndentationRule, SerializedOnEnterRule, SerializedRegExp } from '../common'; +import { SyntaxTokenType } from './types-impl'; export function serializeEnterRules(rules?: theia.OnEnterRule[]): SerializedOnEnterRule[] | undefined { if (typeof rules === 'undefined' || rules === null) { @@ -54,3 +55,16 @@ export function serializeIndentation(indentationRules?: theia.IndentationRule): unIndentedLinePattern: serializeRegExp(indentationRules.unIndentedLinePattern) }; } + +export function serializeAutoClosingPairs(pairs: theia.AutoClosingPair[] | undefined): SerializedAutoClosingPair[] | undefined { + if (!pairs) { + return undefined + }; + return pairs.map(pair => { + return { + open: pair.open, + close: pair.close, + notIn: pair.notIn ? pair.notIn.map(tokenType => SyntaxTokenType.toString(tokenType)) : undefined + } + }); +} \ No newline at end of file diff --git a/packages/plugin-ext/src/plugin/languages.ts b/packages/plugin-ext/src/plugin/languages.ts index 4c76438096351..1eab19d5b55fa 100644 --- a/packages/plugin-ext/src/plugin/languages.ts +++ b/packages/plugin-ext/src/plugin/languages.ts @@ -108,7 +108,7 @@ import { isReadonlyArray } from '../common/arrays'; import { DisposableCollection, disposableTimeout, Disposable as TheiaDisposable } from '@theia/core/lib/common/disposable'; import { Severity } from '@theia/core/lib/common/severity'; import { LinkedEditingRangeAdapter } from './languages/linked-editing-range'; -import { serializeEnterRules, serializeIndentation, serializeRegExp } from './languages-utils'; +import { serializeAutoClosingPairs, serializeEnterRules, serializeIndentation, serializeRegExp } from './languages-utils'; import { InlayHintsAdapter } from './languages/inlay-hints'; import { InlineCompletionAdapter, InlineCompletionAdapterBase } from './languages/inline-completion'; import { DocumentDropEditAdapter } from './languages/document-drop-edit'; @@ -208,7 +208,8 @@ export class LanguagesExtImpl implements LanguagesExt { comments: configuration.comments, onEnterRules: serializeEnterRules(configuration.onEnterRules), wordPattern: serializeRegExp(configuration.wordPattern), - indentationRules: serializeIndentation(configuration.indentationRules) + indentationRules: serializeIndentation(configuration.indentationRules), + autoClosingPairs: serializeAutoClosingPairs(configuration.autoClosingPairs) }; this.proxy.$setLanguageConfiguration(callId, language, config); diff --git a/packages/plugin-ext/src/plugin/plugin-context.ts b/packages/plugin-ext/src/plugin/plugin-context.ts index f972127dbec48..60c357b66e25a 100644 --- a/packages/plugin-ext/src/plugin/plugin-context.ts +++ b/packages/plugin-ext/src/plugin/plugin-context.ts @@ -57,6 +57,7 @@ import { StatusBarAlignment, RelativePattern, IndentAction, + SyntaxTokenType, CompletionItem, CompletionItemKind, CompletionList, @@ -1225,6 +1226,7 @@ export function createAPIFactory( ConfigurationTarget, RelativePattern, IndentAction, + SyntaxTokenType, CompletionItem, CompletionItemKind, CompletionList, diff --git a/packages/plugin-ext/src/plugin/types-impl.ts b/packages/plugin-ext/src/plugin/types-impl.ts index 6550b577f1172..0ef339ae03b8f 100644 --- a/packages/plugin-ext/src/plugin/types-impl.ts +++ b/packages/plugin-ext/src/plugin/types-impl.ts @@ -845,6 +845,38 @@ export enum IndentAction { Outdent = 3 } +export namespace SyntaxTokenType { + export function toString(v: SyntaxTokenType | unknown): 'other' | 'comment' | 'string' | 'regex' { + switch (v) { + case SyntaxTokenType.Other: return 'other'; + case SyntaxTokenType.Comment: return 'comment'; + case SyntaxTokenType.String: return 'string'; + case SyntaxTokenType.RegEx: return 'regex'; + } + return 'other'; + } +} + +export enum SyntaxTokenType { + /** + * Everything except tokens that are part of comments, string literals and regular expressions. + */ + Other = 0, + /** + * A comment. + */ + Comment = 1, + /** + * A string literal. + */ + String = 2, + /** + * A regular expression. + */ + RegEx = 3 +} + + @es5ClassCompat export class TextEdit { diff --git a/packages/plugin/src/theia.d.ts b/packages/plugin/src/theia.d.ts index 15e5c628c7c90..dab5c10d6ab18 100644 --- a/packages/plugin/src/theia.d.ts +++ b/packages/plugin/src/theia.d.ts @@ -8038,6 +8038,43 @@ export module '@theia/plugin' { unIndentedLinePattern?: RegExp; } + export enum SyntaxTokenType { + /** + * Everything except tokens that are part of comments, string literals and regular expressions. + */ + Other = 0, + /** + * A comment. + */ + Comment = 1, + /** + * A string literal. + */ + String = 2, + /** + * A regular expression. + */ + RegEx = 3 + } + + /** + * Describes pairs of strings where the close string will be automatically inserted when typing the opening string. + */ + export interface AutoClosingPair { + /** + * The string that will trigger the automatic insertion of the closing string. + */ + open: string; + /** + * The closing string that will be automatically inserted when typing the opening string. + */ + close: string; + /** + * A set of tokens where the pair should not be auto closed. + */ + notIn?: SyntaxTokenType[]; + } + /** * The language configuration interfaces defines the contract between extensions * and various editor features, like automatic bracket insertion, automatic indentation etc. @@ -8077,6 +8114,10 @@ export module '@theia/plugin' { */ onEnterRules?: OnEnterRule[]; + /** + * The language's auto closing pairs. + */ + autoClosingPairs?: AutoClosingPair[]; } /**