0eb13b90e5b091137c7f214a9e5f50028f471c0b70ae7905739050022e71e2ae4a5d26443a3a2b6afaa35d974c1bf4bd208166de26f60a15ed8df832759e25 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. import { Emitter } from '../../../base/common/event.js';
  15. import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';
  16. import * as strings from '../../../base/common/strings.js';
  17. import { DEFAULT_WORD_REGEXP, ensureValidWordDefinition } from '../core/wordHelper.js';
  18. import { AutoClosingPairs } from './languageConfiguration.js';
  19. import { createScopedLineTokens } from './supports.js';
  20. import { CharacterPairSupport } from './supports/characterPair.js';
  21. import { BracketElectricCharacterSupport } from './supports/electricCharacter.js';
  22. import { IndentRulesSupport } from './supports/indentRules.js';
  23. import { OnEnterSupport } from './supports/onEnter.js';
  24. import { RichEditBrackets } from './supports/richEditBrackets.js';
  25. import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
  26. import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';
  27. import { ILanguageService } from './language.js';
  28. import { registerSingleton } from '../../../platform/instantiation/common/extensions.js';
  29. import { PLAINTEXT_LANGUAGE_ID } from './modesRegistry.js';
  30. import { LanguageBracketsConfiguration } from './supports/languageBracketsConfiguration.js';
  31. export class LanguageConfigurationServiceChangeEvent {
  32. constructor(languageId) {
  33. this.languageId = languageId;
  34. }
  35. affects(languageId) {
  36. return !this.languageId ? true : this.languageId === languageId;
  37. }
  38. }
  39. export const ILanguageConfigurationService = createDecorator('languageConfigurationService');
  40. let LanguageConfigurationService = class LanguageConfigurationService extends Disposable {
  41. constructor(configurationService, languageService) {
  42. super();
  43. this.configurationService = configurationService;
  44. this.languageService = languageService;
  45. this._registry = this._register(new LanguageConfigurationRegistry());
  46. this.onDidChangeEmitter = this._register(new Emitter());
  47. this.onDidChange = this.onDidChangeEmitter.event;
  48. this.configurations = new Map();
  49. const languageConfigKeys = new Set(Object.values(customizedLanguageConfigKeys));
  50. this._register(this.configurationService.onDidChangeConfiguration((e) => {
  51. const globalConfigChanged = e.change.keys.some((k) => languageConfigKeys.has(k));
  52. const localConfigChanged = e.change.overrides
  53. .filter(([overrideLangName, keys]) => keys.some((k) => languageConfigKeys.has(k)))
  54. .map(([overrideLangName]) => overrideLangName);
  55. if (globalConfigChanged) {
  56. this.configurations.clear();
  57. this.onDidChangeEmitter.fire(new LanguageConfigurationServiceChangeEvent(undefined));
  58. }
  59. else {
  60. for (const languageId of localConfigChanged) {
  61. if (this.languageService.isRegisteredLanguageId(languageId)) {
  62. this.configurations.delete(languageId);
  63. this.onDidChangeEmitter.fire(new LanguageConfigurationServiceChangeEvent(languageId));
  64. }
  65. }
  66. }
  67. }));
  68. this._register(this._registry.onDidChange((e) => {
  69. this.configurations.delete(e.languageId);
  70. this.onDidChangeEmitter.fire(new LanguageConfigurationServiceChangeEvent(e.languageId));
  71. }));
  72. }
  73. register(languageId, configuration, priority) {
  74. return this._registry.register(languageId, configuration, priority);
  75. }
  76. getLanguageConfiguration(languageId) {
  77. let result = this.configurations.get(languageId);
  78. if (!result) {
  79. result = computeConfig(languageId, this._registry, this.configurationService, this.languageService);
  80. this.configurations.set(languageId, result);
  81. }
  82. return result;
  83. }
  84. };
  85. LanguageConfigurationService = __decorate([
  86. __param(0, IConfigurationService),
  87. __param(1, ILanguageService)
  88. ], LanguageConfigurationService);
  89. export { LanguageConfigurationService };
  90. function computeConfig(languageId, registry, configurationService, languageService) {
  91. let languageConfig = registry.getLanguageConfiguration(languageId);
  92. if (!languageConfig) {
  93. if (!languageService.isRegisteredLanguageId(languageId)) {
  94. throw new Error(`Language id "${languageId}" is not configured nor known`);
  95. }
  96. languageConfig = new ResolvedLanguageConfiguration(languageId, {});
  97. }
  98. const customizedConfig = getCustomizedLanguageConfig(languageConfig.languageId, configurationService);
  99. const data = combineLanguageConfigurations([languageConfig.underlyingConfig, customizedConfig]);
  100. const config = new ResolvedLanguageConfiguration(languageConfig.languageId, data);
  101. return config;
  102. }
  103. const customizedLanguageConfigKeys = {
  104. brackets: 'editor.language.brackets',
  105. colorizedBracketPairs: 'editor.language.colorizedBracketPairs'
  106. };
  107. function getCustomizedLanguageConfig(languageId, configurationService) {
  108. const brackets = configurationService.getValue(customizedLanguageConfigKeys.brackets, {
  109. overrideIdentifier: languageId,
  110. });
  111. const colorizedBracketPairs = configurationService.getValue(customizedLanguageConfigKeys.colorizedBracketPairs, {
  112. overrideIdentifier: languageId,
  113. });
  114. return {
  115. brackets: validateBracketPairs(brackets),
  116. colorizedBracketPairs: validateBracketPairs(colorizedBracketPairs),
  117. };
  118. }
  119. function validateBracketPairs(data) {
  120. if (!Array.isArray(data)) {
  121. return undefined;
  122. }
  123. return data.map(pair => {
  124. if (!Array.isArray(pair) || pair.length !== 2) {
  125. return undefined;
  126. }
  127. return [pair[0], pair[1]];
  128. }).filter((p) => !!p);
  129. }
  130. export function getIndentationAtPosition(model, lineNumber, column) {
  131. const lineText = model.getLineContent(lineNumber);
  132. let indentation = strings.getLeadingWhitespace(lineText);
  133. if (indentation.length > column - 1) {
  134. indentation = indentation.substring(0, column - 1);
  135. }
  136. return indentation;
  137. }
  138. export function getScopedLineTokens(model, lineNumber, columnNumber) {
  139. model.tokenization.forceTokenization(lineNumber);
  140. const lineTokens = model.tokenization.getLineTokens(lineNumber);
  141. const column = (typeof columnNumber === 'undefined' ? model.getLineMaxColumn(lineNumber) - 1 : columnNumber - 1);
  142. return createScopedLineTokens(lineTokens, column);
  143. }
  144. class ComposedLanguageConfiguration {
  145. constructor(languageId) {
  146. this.languageId = languageId;
  147. this._resolved = null;
  148. this._entries = [];
  149. this._order = 0;
  150. this._resolved = null;
  151. }
  152. register(configuration, priority) {
  153. const entry = new LanguageConfigurationContribution(configuration, priority, ++this._order);
  154. this._entries.push(entry);
  155. this._resolved = null;
  156. return toDisposable(() => {
  157. for (let i = 0; i < this._entries.length; i++) {
  158. if (this._entries[i] === entry) {
  159. this._entries.splice(i, 1);
  160. this._resolved = null;
  161. break;
  162. }
  163. }
  164. });
  165. }
  166. getResolvedConfiguration() {
  167. if (!this._resolved) {
  168. const config = this._resolve();
  169. if (config) {
  170. this._resolved = new ResolvedLanguageConfiguration(this.languageId, config);
  171. }
  172. }
  173. return this._resolved;
  174. }
  175. _resolve() {
  176. if (this._entries.length === 0) {
  177. return null;
  178. }
  179. this._entries.sort(LanguageConfigurationContribution.cmp);
  180. return combineLanguageConfigurations(this._entries.map(e => e.configuration));
  181. }
  182. }
  183. function combineLanguageConfigurations(configs) {
  184. let result = {
  185. comments: undefined,
  186. brackets: undefined,
  187. wordPattern: undefined,
  188. indentationRules: undefined,
  189. onEnterRules: undefined,
  190. autoClosingPairs: undefined,
  191. surroundingPairs: undefined,
  192. autoCloseBefore: undefined,
  193. folding: undefined,
  194. colorizedBracketPairs: undefined,
  195. __electricCharacterSupport: undefined,
  196. };
  197. for (const entry of configs) {
  198. result = {
  199. comments: entry.comments || result.comments,
  200. brackets: entry.brackets || result.brackets,
  201. wordPattern: entry.wordPattern || result.wordPattern,
  202. indentationRules: entry.indentationRules || result.indentationRules,
  203. onEnterRules: entry.onEnterRules || result.onEnterRules,
  204. autoClosingPairs: entry.autoClosingPairs || result.autoClosingPairs,
  205. surroundingPairs: entry.surroundingPairs || result.surroundingPairs,
  206. autoCloseBefore: entry.autoCloseBefore || result.autoCloseBefore,
  207. folding: entry.folding || result.folding,
  208. colorizedBracketPairs: entry.colorizedBracketPairs || result.colorizedBracketPairs,
  209. __electricCharacterSupport: entry.__electricCharacterSupport || result.__electricCharacterSupport,
  210. };
  211. }
  212. return result;
  213. }
  214. class LanguageConfigurationContribution {
  215. constructor(configuration, priority, order) {
  216. this.configuration = configuration;
  217. this.priority = priority;
  218. this.order = order;
  219. }
  220. static cmp(a, b) {
  221. if (a.priority === b.priority) {
  222. // higher order last
  223. return a.order - b.order;
  224. }
  225. // higher priority last
  226. return a.priority - b.priority;
  227. }
  228. }
  229. export class LanguageConfigurationChangeEvent {
  230. constructor(languageId) {
  231. this.languageId = languageId;
  232. }
  233. }
  234. export class LanguageConfigurationRegistry extends Disposable {
  235. constructor() {
  236. super();
  237. this._entries = new Map();
  238. this._onDidChange = this._register(new Emitter());
  239. this.onDidChange = this._onDidChange.event;
  240. this._register(this.register(PLAINTEXT_LANGUAGE_ID, {
  241. brackets: [
  242. ['(', ')'],
  243. ['[', ']'],
  244. ['{', '}'],
  245. ],
  246. surroundingPairs: [
  247. { open: '{', close: '}' },
  248. { open: '[', close: ']' },
  249. { open: '(', close: ')' },
  250. { open: '<', close: '>' },
  251. { open: '\"', close: '\"' },
  252. { open: '\'', close: '\'' },
  253. { open: '`', close: '`' },
  254. ],
  255. colorizedBracketPairs: [],
  256. folding: {
  257. offSide: true
  258. }
  259. }, 0));
  260. }
  261. /**
  262. * @param priority Use a higher number for higher priority
  263. */
  264. register(languageId, configuration, priority = 0) {
  265. let entries = this._entries.get(languageId);
  266. if (!entries) {
  267. entries = new ComposedLanguageConfiguration(languageId);
  268. this._entries.set(languageId, entries);
  269. }
  270. const disposable = entries.register(configuration, priority);
  271. this._onDidChange.fire(new LanguageConfigurationChangeEvent(languageId));
  272. return toDisposable(() => {
  273. disposable.dispose();
  274. this._onDidChange.fire(new LanguageConfigurationChangeEvent(languageId));
  275. });
  276. }
  277. getLanguageConfiguration(languageId) {
  278. const entries = this._entries.get(languageId);
  279. return (entries === null || entries === void 0 ? void 0 : entries.getResolvedConfiguration()) || null;
  280. }
  281. }
  282. /**
  283. * Immutable.
  284. */
  285. export class ResolvedLanguageConfiguration {
  286. constructor(languageId, underlyingConfig) {
  287. this.languageId = languageId;
  288. this.underlyingConfig = underlyingConfig;
  289. this._brackets = null;
  290. this._electricCharacter = null;
  291. this._onEnterSupport =
  292. this.underlyingConfig.brackets ||
  293. this.underlyingConfig.indentationRules ||
  294. this.underlyingConfig.onEnterRules
  295. ? new OnEnterSupport(this.underlyingConfig)
  296. : null;
  297. this.comments = ResolvedLanguageConfiguration._handleComments(this.underlyingConfig);
  298. this.characterPair = new CharacterPairSupport(this.underlyingConfig);
  299. this.wordDefinition = this.underlyingConfig.wordPattern || DEFAULT_WORD_REGEXP;
  300. this.indentationRules = this.underlyingConfig.indentationRules;
  301. if (this.underlyingConfig.indentationRules) {
  302. this.indentRulesSupport = new IndentRulesSupport(this.underlyingConfig.indentationRules);
  303. }
  304. else {
  305. this.indentRulesSupport = null;
  306. }
  307. this.foldingRules = this.underlyingConfig.folding || {};
  308. this.bracketsNew = new LanguageBracketsConfiguration(languageId, this.underlyingConfig);
  309. }
  310. getWordDefinition() {
  311. return ensureValidWordDefinition(this.wordDefinition);
  312. }
  313. get brackets() {
  314. if (!this._brackets && this.underlyingConfig.brackets) {
  315. this._brackets = new RichEditBrackets(this.languageId, this.underlyingConfig.brackets);
  316. }
  317. return this._brackets;
  318. }
  319. get electricCharacter() {
  320. if (!this._electricCharacter) {
  321. this._electricCharacter = new BracketElectricCharacterSupport(this.brackets);
  322. }
  323. return this._electricCharacter;
  324. }
  325. onEnter(autoIndent, previousLineText, beforeEnterText, afterEnterText) {
  326. if (!this._onEnterSupport) {
  327. return null;
  328. }
  329. return this._onEnterSupport.onEnter(autoIndent, previousLineText, beforeEnterText, afterEnterText);
  330. }
  331. getAutoClosingPairs() {
  332. return new AutoClosingPairs(this.characterPair.getAutoClosingPairs());
  333. }
  334. getAutoCloseBeforeSet() {
  335. return this.characterPair.getAutoCloseBeforeSet();
  336. }
  337. getSurroundingPairs() {
  338. return this.characterPair.getSurroundingPairs();
  339. }
  340. static _handleComments(conf) {
  341. const commentRule = conf.comments;
  342. if (!commentRule) {
  343. return null;
  344. }
  345. // comment configuration
  346. const comments = {};
  347. if (commentRule.lineComment) {
  348. comments.lineCommentToken = commentRule.lineComment;
  349. }
  350. if (commentRule.blockComment) {
  351. const [blockStart, blockEnd] = commentRule.blockComment;
  352. comments.blockCommentStartToken = blockStart;
  353. comments.blockCommentEndToken = blockEnd;
  354. }
  355. return comments;
  356. }
  357. }
  358. registerSingleton(ILanguageConfigurationService, LanguageConfigurationService);