b51d6add293e3c9bc2cfc6622aa1822572276935402287805b459d36f7d701e48ba84ca1c938463af9c4627f6c6e8307b4b5f60c70cad3567daa6c1202c0d4 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. import * as arrays from '../../../../base/common/arrays.js';
  24. import { createCancelablePromise, Delayer, first } from '../../../../base/common/async.js';
  25. import { CancellationToken } from '../../../../base/common/cancellation.js';
  26. import { Color } from '../../../../base/common/color.js';
  27. import { isCancellationError, onUnexpectedError, onUnexpectedExternalError } from '../../../../base/common/errors.js';
  28. import { Event } from '../../../../base/common/event.js';
  29. import { Disposable, DisposableStore } from '../../../../base/common/lifecycle.js';
  30. import * as strings from '../../../../base/common/strings.js';
  31. import { URI } from '../../../../base/common/uri.js';
  32. import { EditorAction, EditorCommand, registerEditorAction, registerEditorCommand, registerEditorContribution, registerModelAndPositionCommand } from '../../../browser/editorExtensions.js';
  33. import { ICodeEditorService } from '../../../browser/services/codeEditorService.js';
  34. import { Position } from '../../../common/core/position.js';
  35. import { Range } from '../../../common/core/range.js';
  36. import { EditorContextKeys } from '../../../common/editorContextKeys.js';
  37. import { ModelDecorationOptions } from '../../../common/model/textModel.js';
  38. import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';
  39. import * as nls from '../../../../nls.js';
  40. import { ContextKeyExpr, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
  41. import { registerColor } from '../../../../platform/theme/common/colorRegistry.js';
  42. import { registerThemingParticipant } from '../../../../platform/theme/common/themeService.js';
  43. import { ILanguageFeaturesService } from '../../../common/services/languageFeatures.js';
  44. import { ILanguageFeatureDebounceService } from '../../../common/services/languageFeatureDebounce.js';
  45. import { StopWatch } from '../../../../base/common/stopwatch.js';
  46. export const CONTEXT_ONTYPE_RENAME_INPUT_VISIBLE = new RawContextKey('LinkedEditingInputVisible', false);
  47. const DECORATION_CLASS_NAME = 'linked-editing-decoration';
  48. let LinkedEditingContribution = class LinkedEditingContribution extends Disposable {
  49. constructor(editor, contextKeyService, languageFeaturesService, languageConfigurationService, languageFeatureDebounceService) {
  50. super();
  51. this.languageConfigurationService = languageConfigurationService;
  52. this._syncRangesToken = 0;
  53. this._localToDispose = this._register(new DisposableStore());
  54. this._editor = editor;
  55. this._providers = languageFeaturesService.linkedEditingRangeProvider;
  56. this._enabled = false;
  57. this._visibleContextKey = CONTEXT_ONTYPE_RENAME_INPUT_VISIBLE.bindTo(contextKeyService);
  58. this._debounceInformation = languageFeatureDebounceService.for(this._providers, 'Linked Editing', { min: 200 });
  59. this._currentDecorations = this._editor.createDecorationsCollection();
  60. this._languageWordPattern = null;
  61. this._currentWordPattern = null;
  62. this._ignoreChangeEvent = false;
  63. this._localToDispose = this._register(new DisposableStore());
  64. this._rangeUpdateTriggerPromise = null;
  65. this._rangeSyncTriggerPromise = null;
  66. this._currentRequest = null;
  67. this._currentRequestPosition = null;
  68. this._currentRequestModelVersion = null;
  69. this._register(this._editor.onDidChangeModel(() => this.reinitialize(true)));
  70. this._register(this._editor.onDidChangeConfiguration(e => {
  71. if (e.hasChanged(64 /* EditorOption.linkedEditing */) || e.hasChanged(84 /* EditorOption.renameOnType */)) {
  72. this.reinitialize(false);
  73. }
  74. }));
  75. this._register(this._providers.onDidChange(() => this.reinitialize(false)));
  76. this._register(this._editor.onDidChangeModelLanguage(() => this.reinitialize(true)));
  77. this.reinitialize(true);
  78. }
  79. static get(editor) {
  80. return editor.getContribution(LinkedEditingContribution.ID);
  81. }
  82. reinitialize(forceRefresh) {
  83. const model = this._editor.getModel();
  84. const isEnabled = model !== null && (this._editor.getOption(64 /* EditorOption.linkedEditing */) || this._editor.getOption(84 /* EditorOption.renameOnType */)) && this._providers.has(model);
  85. if (isEnabled === this._enabled && !forceRefresh) {
  86. return;
  87. }
  88. this._enabled = isEnabled;
  89. this.clearRanges();
  90. this._localToDispose.clear();
  91. if (!isEnabled || model === null) {
  92. return;
  93. }
  94. this._localToDispose.add(Event.runAndSubscribe(model.onDidChangeLanguageConfiguration, () => {
  95. this._languageWordPattern = this.languageConfigurationService.getLanguageConfiguration(model.getLanguageId()).getWordDefinition();
  96. }));
  97. const rangeUpdateScheduler = new Delayer(this._debounceInformation.get(model));
  98. const triggerRangeUpdate = () => {
  99. var _a;
  100. this._rangeUpdateTriggerPromise = rangeUpdateScheduler.trigger(() => this.updateRanges(), (_a = this._debounceDuration) !== null && _a !== void 0 ? _a : this._debounceInformation.get(model));
  101. };
  102. const rangeSyncScheduler = new Delayer(0);
  103. const triggerRangeSync = (token) => {
  104. this._rangeSyncTriggerPromise = rangeSyncScheduler.trigger(() => this._syncRanges(token));
  105. };
  106. this._localToDispose.add(this._editor.onDidChangeCursorPosition(() => {
  107. triggerRangeUpdate();
  108. }));
  109. this._localToDispose.add(this._editor.onDidChangeModelContent((e) => {
  110. if (!this._ignoreChangeEvent) {
  111. if (this._currentDecorations.length > 0) {
  112. const referenceRange = this._currentDecorations.getRange(0);
  113. if (referenceRange && e.changes.every(c => referenceRange.intersectRanges(c.range))) {
  114. triggerRangeSync(this._syncRangesToken);
  115. return;
  116. }
  117. }
  118. }
  119. triggerRangeUpdate();
  120. }));
  121. this._localToDispose.add({
  122. dispose: () => {
  123. rangeUpdateScheduler.dispose();
  124. rangeSyncScheduler.dispose();
  125. }
  126. });
  127. this.updateRanges();
  128. }
  129. _syncRanges(token) {
  130. // dalayed invocation, make sure we're still on
  131. if (!this._editor.hasModel() || token !== this._syncRangesToken || this._currentDecorations.length === 0) {
  132. // nothing to do
  133. return;
  134. }
  135. const model = this._editor.getModel();
  136. const referenceRange = this._currentDecorations.getRange(0);
  137. if (!referenceRange || referenceRange.startLineNumber !== referenceRange.endLineNumber) {
  138. return this.clearRanges();
  139. }
  140. const referenceValue = model.getValueInRange(referenceRange);
  141. if (this._currentWordPattern) {
  142. const match = referenceValue.match(this._currentWordPattern);
  143. const matchLength = match ? match[0].length : 0;
  144. if (matchLength !== referenceValue.length) {
  145. return this.clearRanges();
  146. }
  147. }
  148. const edits = [];
  149. for (let i = 1, len = this._currentDecorations.length; i < len; i++) {
  150. const mirrorRange = this._currentDecorations.getRange(i);
  151. if (!mirrorRange) {
  152. continue;
  153. }
  154. if (mirrorRange.startLineNumber !== mirrorRange.endLineNumber) {
  155. edits.push({
  156. range: mirrorRange,
  157. text: referenceValue
  158. });
  159. }
  160. else {
  161. let oldValue = model.getValueInRange(mirrorRange);
  162. let newValue = referenceValue;
  163. let rangeStartColumn = mirrorRange.startColumn;
  164. let rangeEndColumn = mirrorRange.endColumn;
  165. const commonPrefixLength = strings.commonPrefixLength(oldValue, newValue);
  166. rangeStartColumn += commonPrefixLength;
  167. oldValue = oldValue.substr(commonPrefixLength);
  168. newValue = newValue.substr(commonPrefixLength);
  169. const commonSuffixLength = strings.commonSuffixLength(oldValue, newValue);
  170. rangeEndColumn -= commonSuffixLength;
  171. oldValue = oldValue.substr(0, oldValue.length - commonSuffixLength);
  172. newValue = newValue.substr(0, newValue.length - commonSuffixLength);
  173. if (rangeStartColumn !== rangeEndColumn || newValue.length !== 0) {
  174. edits.push({
  175. range: new Range(mirrorRange.startLineNumber, rangeStartColumn, mirrorRange.endLineNumber, rangeEndColumn),
  176. text: newValue
  177. });
  178. }
  179. }
  180. }
  181. if (edits.length === 0) {
  182. return;
  183. }
  184. try {
  185. this._editor.popUndoStop();
  186. this._ignoreChangeEvent = true;
  187. const prevEditOperationType = this._editor._getViewModel().getPrevEditOperationType();
  188. this._editor.executeEdits('linkedEditing', edits);
  189. this._editor._getViewModel().setPrevEditOperationType(prevEditOperationType);
  190. }
  191. finally {
  192. this._ignoreChangeEvent = false;
  193. }
  194. }
  195. dispose() {
  196. this.clearRanges();
  197. super.dispose();
  198. }
  199. clearRanges() {
  200. this._visibleContextKey.set(false);
  201. this._currentDecorations.clear();
  202. if (this._currentRequest) {
  203. this._currentRequest.cancel();
  204. this._currentRequest = null;
  205. this._currentRequestPosition = null;
  206. }
  207. }
  208. updateRanges(force = false) {
  209. return __awaiter(this, void 0, void 0, function* () {
  210. if (!this._editor.hasModel()) {
  211. this.clearRanges();
  212. return;
  213. }
  214. const position = this._editor.getPosition();
  215. if (!this._enabled && !force || this._editor.getSelections().length > 1) {
  216. // disabled or multicursor
  217. this.clearRanges();
  218. return;
  219. }
  220. const model = this._editor.getModel();
  221. const modelVersionId = model.getVersionId();
  222. if (this._currentRequestPosition && this._currentRequestModelVersion === modelVersionId) {
  223. if (position.equals(this._currentRequestPosition)) {
  224. return; // same position
  225. }
  226. if (this._currentDecorations.length > 0) {
  227. const range = this._currentDecorations.getRange(0);
  228. if (range && range.containsPosition(position)) {
  229. return; // just moving inside the existing primary range
  230. }
  231. }
  232. }
  233. this._currentRequestPosition = position;
  234. this._currentRequestModelVersion = modelVersionId;
  235. const request = createCancelablePromise((token) => __awaiter(this, void 0, void 0, function* () {
  236. try {
  237. const sw = new StopWatch(false);
  238. const response = yield getLinkedEditingRanges(this._providers, model, position, token);
  239. this._debounceInformation.update(model, sw.elapsed());
  240. if (request !== this._currentRequest) {
  241. return;
  242. }
  243. this._currentRequest = null;
  244. if (modelVersionId !== model.getVersionId()) {
  245. return;
  246. }
  247. let ranges = [];
  248. if (response === null || response === void 0 ? void 0 : response.ranges) {
  249. ranges = response.ranges;
  250. }
  251. this._currentWordPattern = (response === null || response === void 0 ? void 0 : response.wordPattern) || this._languageWordPattern;
  252. let foundReferenceRange = false;
  253. for (let i = 0, len = ranges.length; i < len; i++) {
  254. if (Range.containsPosition(ranges[i], position)) {
  255. foundReferenceRange = true;
  256. if (i !== 0) {
  257. const referenceRange = ranges[i];
  258. ranges.splice(i, 1);
  259. ranges.unshift(referenceRange);
  260. }
  261. break;
  262. }
  263. }
  264. if (!foundReferenceRange) {
  265. // Cannot do linked editing if the ranges are not where the cursor is...
  266. this.clearRanges();
  267. return;
  268. }
  269. const decorations = ranges.map(range => ({ range: range, options: LinkedEditingContribution.DECORATION }));
  270. this._visibleContextKey.set(true);
  271. this._currentDecorations.set(decorations);
  272. this._syncRangesToken++; // cancel any pending syncRanges call
  273. }
  274. catch (err) {
  275. if (!isCancellationError(err)) {
  276. onUnexpectedError(err);
  277. }
  278. if (this._currentRequest === request || !this._currentRequest) {
  279. // stop if we are still the latest request
  280. this.clearRanges();
  281. }
  282. }
  283. }));
  284. this._currentRequest = request;
  285. return request;
  286. });
  287. }
  288. };
  289. LinkedEditingContribution.ID = 'editor.contrib.linkedEditing';
  290. LinkedEditingContribution.DECORATION = ModelDecorationOptions.register({
  291. description: 'linked-editing',
  292. stickiness: 0 /* TrackedRangeStickiness.AlwaysGrowsWhenTypingAtEdges */,
  293. className: DECORATION_CLASS_NAME
  294. });
  295. LinkedEditingContribution = __decorate([
  296. __param(1, IContextKeyService),
  297. __param(2, ILanguageFeaturesService),
  298. __param(3, ILanguageConfigurationService),
  299. __param(4, ILanguageFeatureDebounceService)
  300. ], LinkedEditingContribution);
  301. export { LinkedEditingContribution };
  302. export class LinkedEditingAction extends EditorAction {
  303. constructor() {
  304. super({
  305. id: 'editor.action.linkedEditing',
  306. label: nls.localize('linkedEditing.label', "Start Linked Editing"),
  307. alias: 'Start Linked Editing',
  308. precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasRenameProvider),
  309. kbOpts: {
  310. kbExpr: EditorContextKeys.editorTextFocus,
  311. primary: 2048 /* KeyMod.CtrlCmd */ | 1024 /* KeyMod.Shift */ | 60 /* KeyCode.F2 */,
  312. weight: 100 /* KeybindingWeight.EditorContrib */
  313. }
  314. });
  315. }
  316. runCommand(accessor, args) {
  317. const editorService = accessor.get(ICodeEditorService);
  318. const [uri, pos] = Array.isArray(args) && args || [undefined, undefined];
  319. if (URI.isUri(uri) && Position.isIPosition(pos)) {
  320. return editorService.openCodeEditor({ resource: uri }, editorService.getActiveCodeEditor()).then(editor => {
  321. if (!editor) {
  322. return;
  323. }
  324. editor.setPosition(pos);
  325. editor.invokeWithinContext(accessor => {
  326. this.reportTelemetry(accessor, editor);
  327. return this.run(accessor, editor);
  328. });
  329. }, onUnexpectedError);
  330. }
  331. return super.runCommand(accessor, args);
  332. }
  333. run(_accessor, editor) {
  334. const controller = LinkedEditingContribution.get(editor);
  335. if (controller) {
  336. return Promise.resolve(controller.updateRanges(true));
  337. }
  338. return Promise.resolve();
  339. }
  340. }
  341. const LinkedEditingCommand = EditorCommand.bindToContribution(LinkedEditingContribution.get);
  342. registerEditorCommand(new LinkedEditingCommand({
  343. id: 'cancelLinkedEditingInput',
  344. precondition: CONTEXT_ONTYPE_RENAME_INPUT_VISIBLE,
  345. handler: x => x.clearRanges(),
  346. kbOpts: {
  347. kbExpr: EditorContextKeys.editorTextFocus,
  348. weight: 100 /* KeybindingWeight.EditorContrib */ + 99,
  349. primary: 9 /* KeyCode.Escape */,
  350. secondary: [1024 /* KeyMod.Shift */ | 9 /* KeyCode.Escape */]
  351. }
  352. }));
  353. function getLinkedEditingRanges(providers, model, position, token) {
  354. const orderedByScore = providers.ordered(model);
  355. // in order of score ask the linked editing range provider
  356. // until someone response with a good result
  357. // (good = not null)
  358. return first(orderedByScore.map(provider => () => __awaiter(this, void 0, void 0, function* () {
  359. try {
  360. return yield provider.provideLinkedEditingRanges(model, position, token);
  361. }
  362. catch (e) {
  363. onUnexpectedExternalError(e);
  364. return undefined;
  365. }
  366. })), result => !!result && arrays.isNonEmptyArray(result === null || result === void 0 ? void 0 : result.ranges));
  367. }
  368. export const editorLinkedEditingBackground = registerColor('editor.linkedEditingBackground', { dark: Color.fromHex('#f00').transparent(0.3), light: Color.fromHex('#f00').transparent(0.3), hcDark: Color.fromHex('#f00').transparent(0.3), hcLight: Color.white }, nls.localize('editorLinkedEditingBackground', 'Background color when the editor auto renames on type.'));
  369. registerThemingParticipant((theme, collector) => {
  370. const editorLinkedEditingBackgroundColor = theme.getColor(editorLinkedEditingBackground);
  371. if (editorLinkedEditingBackgroundColor) {
  372. collector.addRule(`.monaco-editor .${DECORATION_CLASS_NAME} { background: ${editorLinkedEditingBackgroundColor}; border-left-color: ${editorLinkedEditingBackgroundColor}; }`);
  373. }
  374. });
  375. registerModelAndPositionCommand('_executeLinkedEditingProvider', (_accessor, model, position) => {
  376. const { linkedEditingRangeProvider } = _accessor.get(ILanguageFeaturesService);
  377. return getLinkedEditingRanges(linkedEditingRangeProvider, model, position, CancellationToken.None);
  378. });
  379. registerEditorContribution(LinkedEditingContribution.ID, LinkedEditingContribution);
  380. registerEditorAction(LinkedEditingAction);