Skip to content

Commit

Permalink
Add autoClosingPairs field to LanguageConfiguration API
Browse files Browse the repository at this point in the history
Contributed on behalf of STMicroelectronics

Signed-off-by: Thomas Mäder <[email protected]>
  • Loading branch information
tsmaeder committed Nov 22, 2023
1 parent bf93b29 commit 0b56533
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/main/browser/languages-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
16 changes: 15 additions & 1 deletion packages/plugin-ext/src/plugin/languages-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
});
}
5 changes: 3 additions & 2 deletions packages/plugin-ext/src/plugin/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
StatusBarAlignment,
RelativePattern,
IndentAction,
SyntaxTokenType,
CompletionItem,
CompletionItemKind,
CompletionList,
Expand Down Expand Up @@ -1225,6 +1226,7 @@ export function createAPIFactory(
ConfigurationTarget,
RelativePattern,
IndentAction,
SyntaxTokenType,
CompletionItem,
CompletionItemKind,
CompletionList,
Expand Down
32 changes: 32 additions & 0 deletions packages/plugin-ext/src/plugin/types-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
41 changes: 41 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -8077,6 +8114,10 @@ export module '@theia/plugin' {
*/
onEnterRules?: OnEnterRule[];

/**
* The language's auto closing pairs.
*/
autoClosingPairs?: AutoClosingPair[];
}

/**
Expand Down

0 comments on commit 0b56533

Please sign in to comment.