Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode: Support languages.configuration.onEnterRules and add OnEnterRule#previousLineText #11225

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import {
TextEditorLineNumbersStyle,
EndOfLine,
OverviewRulerLane,
IndentAction,
FileOperationOptions,
TextDocumentChangeReason,
IndentAction,
} from '../plugin/types-impl';
import { UriComponents } from './uri-components';
import {
Expand Down Expand Up @@ -1307,19 +1307,20 @@ export interface SerializedIndentationRule {
unIndentedLinePattern?: SerializedRegExp;
}

export interface EnterAction {
export interface SerializedOnEnterRule {
beforeText: SerializedRegExp;
afterText?: SerializedRegExp;
previousLineText?: SerializedRegExp;
action: SerializedEnterAction;
}

export interface SerializedEnterAction {
indentAction: IndentAction;
outdentCurrentLine?: boolean;
appendText?: string;
removeText?: number;
}

export interface SerializedOnEnterRule {
beforeText: SerializedRegExp;
afterText?: SerializedRegExp;
action: EnterAction;
}

export interface SerializedLanguageConfiguration {
comments?: CommentRule;
brackets?: CharacterPair[];
Expand Down
15 changes: 15 additions & 0 deletions packages/plugin-ext/src/common/plugin-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export interface PluginPackageLanguageContributionConfiguration {
wordPattern?: string;
indentationRules?: IndentationRules;
folding?: FoldingRules;
onEnterRules?: OnEnterRule[];
}

export interface PluginTaskDefinitionContribution {
Expand Down Expand Up @@ -622,6 +623,7 @@ export interface LanguageConfiguration {
comments?: CommentRule;
folding?: FoldingRules;
wordPattern?: string;
onEnterRules?: OnEnterRule[];
}

/**
Expand Down Expand Up @@ -670,6 +672,19 @@ export interface FoldingRules {
markers?: FoldingMarkers;
}

export interface OnEnterRule {
beforeText: string;
afterText?: string;
previousLineText?: string;
action: EnterAction;
}

export interface EnterAction {
indent: 'none' | 'indent' | 'outdent' | 'indentOutdent';
appendText?: string;
removeText?: number;
}

/**
* Custom Editors contribution
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,8 @@ export class TheiaPluginScanner implements PluginScanner {
wordPattern: rawConfiguration.wordPattern,
autoClosingPairs: this.extractValidAutoClosingPairs(rawLang.id, rawConfiguration),
indentationRules: rawConfiguration.indentationRules,
surroundingPairs: this.extractValidSurroundingPairs(rawLang.id, rawConfiguration)
surroundingPairs: this.extractValidSurroundingPairs(rawLang.id, rawConfiguration),
onEnterRules: rawConfiguration.onEnterRules,
};
result.configuration = configuration;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/main/browser/languages-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,8 @@ function reviveOnEnterRule(onEnterRule: SerializedOnEnterRule): monaco.languages
return {
beforeText: reviveRegExp(onEnterRule.beforeText)!,
afterText: reviveRegExp(onEnterRule.afterText),
action: onEnterRule.action
previousLineText: reviveRegExp(onEnterRule.previousLineText),
action: onEnterRule.action,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { TextmateRegistry, getEncodedLanguageId, MonacoTextmateService, GrammarD
import { MenusContributionPointHandler } from './menus/menus-contribution-handler';
import { PluginViewRegistry } from './view/plugin-view-registry';
import { PluginCustomEditorRegistry } from './custom-editors/plugin-custom-editor-registry';
import { PluginContribution, IndentationRules, FoldingRules, ScopeMap, DeployedPlugin, GrammarsContribution } from '../../common';
import { PluginContribution, IndentationRules, FoldingRules, ScopeMap, DeployedPlugin, GrammarsContribution, EnterAction, OnEnterRule } from '../../common';
import {
DefaultUriLabelProviderContribution,
LabelProviderContribution,
Expand Down Expand Up @@ -172,7 +172,8 @@ export class PluginContributionHandler {
comments: langConfiguration.comments,
folding: this.convertFolding(langConfiguration.folding),
surroundingPairs: langConfiguration.surroundingPairs,
indentationRules: this.convertIndentationRules(langConfiguration.indentationRules)
indentationRules: this.convertIndentationRules(langConfiguration.indentationRules),
onEnterRules: this.convertOnEnterRules(langConfiguration.onEnterRules),
}));
}
}
Expand Down Expand Up @@ -484,7 +485,6 @@ export class PluginContributionHandler {
}

return result;

}

private convertTokenTypes(tokenTypes?: ScopeMap): ITokenTypeMap | undefined {
Expand Down Expand Up @@ -530,4 +530,42 @@ export class PluginContributionHandler {
return result;
}

private convertOnEnterRules(onEnterRules?: OnEnterRule[]): monaco.languages.OnEnterRule[] | undefined {
if (!onEnterRules) {
return undefined;
}

const result: monaco.languages.OnEnterRule[] = [];
for (const onEnterRule of onEnterRules) {
const rule: monaco.languages.OnEnterRule = {
beforeText: this.createRegex(onEnterRule.beforeText)!,
afterText: this.createRegex(onEnterRule.afterText),
previousLineText: this.createRegex(onEnterRule.previousLineText),
action: this.createEnterAction(onEnterRule.action),
};
result.push(rule);
}

return result;
}

private createEnterAction(action: EnterAction): monaco.languages.EnterAction {
let indentAction: monaco.languages.IndentAction;
switch (action.indent) {
case 'indent':
indentAction = monaco.languages.IndentAction.Indent;
break;
case 'indentOutdent':
indentAction = monaco.languages.IndentAction.IndentOutdent;
break;
case 'outdent':
indentAction = monaco.languages.IndentAction.Outdent;
break;
default:
indentAction = monaco.languages.IndentAction.None;
break;
}
return { indentAction, appendText: action.appendText, removeText: action.removeText };
}

}
5 changes: 3 additions & 2 deletions packages/plugin-ext/src/plugin/languages-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export function serializeEnterRules(rules?: theia.OnEnterRule[]): SerializedOnEn

return rules.map(r =>
({
action: r.action,
beforeText: serializeRegExp(r.beforeText),
afterText: serializeRegExp(r.afterText)
afterText: serializeRegExp(r.afterText),
previousLineText: serializeRegExp(r.previousLineText),
action: r.action,
} as SerializedOnEnterRule));
}

Expand Down
4 changes: 4 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6709,6 +6709,10 @@ export module '@theia/plugin' {
* This rule will only execute if the text after the cursor matches this regular expression.
*/
afterText?: RegExp;
/**
* This rule will only execute if the text above the current line matches this regular expression.
*/
previousLineText?: RegExp;
/**
* The action to execute.
*/
Expand Down