18130a9899999b9e2cacc2491e3a5d050131eedaf4258eab23963852805edfdd9c41b7391e6834054c4511ffa09d1cb9f110db9fe7f99734ea97e7bfab8435 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 { Disposable } from '../../../../base/common/lifecycle.js';
  24. import { basename } from '../../../../base/common/resources.js';
  25. import { registerEditorContribution } from '../../../browser/editorExtensions.js';
  26. import { ICodeEditorService } from '../../../browser/services/codeEditorService.js';
  27. import * as nls from '../../../../nls.js';
  28. import { IDialogService } from '../../../../platform/dialogs/common/dialogs.js';
  29. const ignoreUnusualLineTerminators = 'ignoreUnusualLineTerminators';
  30. function writeIgnoreState(codeEditorService, model, state) {
  31. codeEditorService.setModelProperty(model.uri, ignoreUnusualLineTerminators, state);
  32. }
  33. function readIgnoreState(codeEditorService, model) {
  34. return codeEditorService.getModelProperty(model.uri, ignoreUnusualLineTerminators);
  35. }
  36. let UnusualLineTerminatorsDetector = class UnusualLineTerminatorsDetector extends Disposable {
  37. constructor(_editor, _dialogService, _codeEditorService) {
  38. super();
  39. this._editor = _editor;
  40. this._dialogService = _dialogService;
  41. this._codeEditorService = _codeEditorService;
  42. this._config = this._editor.getOption(116 /* EditorOption.unusualLineTerminators */);
  43. this._register(this._editor.onDidChangeConfiguration((e) => {
  44. if (e.hasChanged(116 /* EditorOption.unusualLineTerminators */)) {
  45. this._config = this._editor.getOption(116 /* EditorOption.unusualLineTerminators */);
  46. this._checkForUnusualLineTerminators();
  47. }
  48. }));
  49. this._register(this._editor.onDidChangeModel(() => {
  50. this._checkForUnusualLineTerminators();
  51. }));
  52. this._register(this._editor.onDidChangeModelContent((e) => {
  53. if (e.isUndoing) {
  54. // skip checking in case of undoing
  55. return;
  56. }
  57. this._checkForUnusualLineTerminators();
  58. }));
  59. }
  60. _checkForUnusualLineTerminators() {
  61. return __awaiter(this, void 0, void 0, function* () {
  62. if (this._config === 'off') {
  63. return;
  64. }
  65. if (!this._editor.hasModel()) {
  66. return;
  67. }
  68. const model = this._editor.getModel();
  69. if (!model.mightContainUnusualLineTerminators()) {
  70. return;
  71. }
  72. const ignoreState = readIgnoreState(this._codeEditorService, model);
  73. if (ignoreState === true) {
  74. // this model should be ignored
  75. return;
  76. }
  77. if (this._editor.getOption(83 /* EditorOption.readOnly */)) {
  78. // read only editor => sorry!
  79. return;
  80. }
  81. if (this._config === 'auto') {
  82. // just do it!
  83. model.removeUnusualLineTerminators(this._editor.getSelections());
  84. return;
  85. }
  86. const result = yield this._dialogService.confirm({
  87. title: nls.localize('unusualLineTerminators.title', "Unusual Line Terminators"),
  88. message: nls.localize('unusualLineTerminators.message', "Detected unusual line terminators"),
  89. detail: nls.localize('unusualLineTerminators.detail', "The file '{0}' contains one or more unusual line terminator characters, like Line Separator (LS) or Paragraph Separator (PS).\n\nIt is recommended to remove them from the file. This can be configured via `editor.unusualLineTerminators`.", basename(model.uri)),
  90. primaryButton: nls.localize('unusualLineTerminators.fix', "Remove Unusual Line Terminators"),
  91. secondaryButton: nls.localize('unusualLineTerminators.ignore', "Ignore")
  92. });
  93. if (!result.confirmed) {
  94. // this model should be ignored
  95. writeIgnoreState(this._codeEditorService, model, true);
  96. return;
  97. }
  98. model.removeUnusualLineTerminators(this._editor.getSelections());
  99. });
  100. }
  101. };
  102. UnusualLineTerminatorsDetector.ID = 'editor.contrib.unusualLineTerminatorsDetector';
  103. UnusualLineTerminatorsDetector = __decorate([
  104. __param(1, IDialogService),
  105. __param(2, ICodeEditorService)
  106. ], UnusualLineTerminatorsDetector);
  107. export { UnusualLineTerminatorsDetector };
  108. registerEditorContribution(UnusualLineTerminatorsDetector.ID, UnusualLineTerminatorsDetector);