Path: blob/main/src/vs/editor/common/languages/supports/electricCharacter.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { distinct } from '../../../../base/common/arrays.js';6import { ScopedLineTokens, ignoreBracketsInToken } from '../supports.js';7import { BracketsUtils, RichEditBrackets } from './richEditBrackets.js';89/**10* Interface used to support electric characters11* @internal12*/13export interface IElectricAction {14// The line will be indented at the same level of the line15// which contains the matching given bracket type.16matchOpenBracket: string;17}1819export class BracketElectricCharacterSupport {2021private readonly _richEditBrackets: RichEditBrackets | null;2223constructor(richEditBrackets: RichEditBrackets | null) {24this._richEditBrackets = richEditBrackets;25}2627public getElectricCharacters(): string[] {28const result: string[] = [];2930if (this._richEditBrackets) {31for (const bracket of this._richEditBrackets.brackets) {32for (const close of bracket.close) {33const lastChar = close.charAt(close.length - 1);34result.push(lastChar);35}36}37}3839return distinct(result);40}4142public onElectricCharacter(character: string, context: ScopedLineTokens, column: number): IElectricAction | null {43if (!this._richEditBrackets || this._richEditBrackets.brackets.length === 0) {44return null;45}4647const tokenIndex = context.findTokenIndexAtOffset(column - 1);48if (ignoreBracketsInToken(context.getStandardTokenType(tokenIndex))) {49return null;50}5152const reversedBracketRegex = this._richEditBrackets.reversedRegex;53const text = context.getLineContent().substring(0, column - 1) + character;5455const r = BracketsUtils.findPrevBracketInRange(reversedBracketRegex, 1, text, 0, text.length);56if (!r) {57return null;58}5960const bracketText = text.substring(r.startColumn - 1, r.endColumn - 1).toLowerCase();6162const isOpen = this._richEditBrackets.textIsOpenBracket[bracketText];63if (isOpen) {64return null;65}6667const textBeforeBracket = context.getActualLineContentBefore(r.startColumn - 1);68if (!/^\s*$/.test(textBeforeBracket)) {69// There is other text on the line before the bracket70return null;71}7273return {74matchOpenBracket: bracketText75};76}77}787980