42e7a4cbf97f3afae4de0b159f4f9f3a683b8d7b1e348d317b78148346529b871d630eb42923c44f162c73da914fd479d0357dda56c0f9ea52f23c5cf83500 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. import { Emitter } from '../../../../base/common/event.js';
  6. import { Disposable } from '../../../../base/common/lifecycle.js';
  7. import { Range } from '../../../common/core/range.js';
  8. import { MATCHES_LIMIT } from './findModel.js';
  9. function effectiveOptionValue(override, value) {
  10. if (override === 1 /* FindOptionOverride.True */) {
  11. return true;
  12. }
  13. if (override === 2 /* FindOptionOverride.False */) {
  14. return false;
  15. }
  16. return value;
  17. }
  18. export class FindReplaceState extends Disposable {
  19. constructor() {
  20. super();
  21. this._onFindReplaceStateChange = this._register(new Emitter());
  22. this.onFindReplaceStateChange = this._onFindReplaceStateChange.event;
  23. this._searchString = '';
  24. this._replaceString = '';
  25. this._isRevealed = false;
  26. this._isReplaceRevealed = false;
  27. this._isRegex = false;
  28. this._isRegexOverride = 0 /* FindOptionOverride.NotSet */;
  29. this._wholeWord = false;
  30. this._wholeWordOverride = 0 /* FindOptionOverride.NotSet */;
  31. this._matchCase = false;
  32. this._matchCaseOverride = 0 /* FindOptionOverride.NotSet */;
  33. this._preserveCase = false;
  34. this._preserveCaseOverride = 0 /* FindOptionOverride.NotSet */;
  35. this._searchScope = null;
  36. this._matchesPosition = 0;
  37. this._matchesCount = 0;
  38. this._currentMatch = null;
  39. this._loop = true;
  40. this._isSearching = false;
  41. this._filters = null;
  42. }
  43. get searchString() { return this._searchString; }
  44. get replaceString() { return this._replaceString; }
  45. get isRevealed() { return this._isRevealed; }
  46. get isReplaceRevealed() { return this._isReplaceRevealed; }
  47. get isRegex() { return effectiveOptionValue(this._isRegexOverride, this._isRegex); }
  48. get wholeWord() { return effectiveOptionValue(this._wholeWordOverride, this._wholeWord); }
  49. get matchCase() { return effectiveOptionValue(this._matchCaseOverride, this._matchCase); }
  50. get preserveCase() { return effectiveOptionValue(this._preserveCaseOverride, this._preserveCase); }
  51. get actualIsRegex() { return this._isRegex; }
  52. get actualWholeWord() { return this._wholeWord; }
  53. get actualMatchCase() { return this._matchCase; }
  54. get actualPreserveCase() { return this._preserveCase; }
  55. get searchScope() { return this._searchScope; }
  56. get matchesPosition() { return this._matchesPosition; }
  57. get matchesCount() { return this._matchesCount; }
  58. get currentMatch() { return this._currentMatch; }
  59. changeMatchInfo(matchesPosition, matchesCount, currentMatch) {
  60. const changeEvent = {
  61. moveCursor: false,
  62. updateHistory: false,
  63. searchString: false,
  64. replaceString: false,
  65. isRevealed: false,
  66. isReplaceRevealed: false,
  67. isRegex: false,
  68. wholeWord: false,
  69. matchCase: false,
  70. preserveCase: false,
  71. searchScope: false,
  72. matchesPosition: false,
  73. matchesCount: false,
  74. currentMatch: false,
  75. loop: false,
  76. isSearching: false,
  77. filters: false
  78. };
  79. let somethingChanged = false;
  80. if (matchesCount === 0) {
  81. matchesPosition = 0;
  82. }
  83. if (matchesPosition > matchesCount) {
  84. matchesPosition = matchesCount;
  85. }
  86. if (this._matchesPosition !== matchesPosition) {
  87. this._matchesPosition = matchesPosition;
  88. changeEvent.matchesPosition = true;
  89. somethingChanged = true;
  90. }
  91. if (this._matchesCount !== matchesCount) {
  92. this._matchesCount = matchesCount;
  93. changeEvent.matchesCount = true;
  94. somethingChanged = true;
  95. }
  96. if (typeof currentMatch !== 'undefined') {
  97. if (!Range.equalsRange(this._currentMatch, currentMatch)) {
  98. this._currentMatch = currentMatch;
  99. changeEvent.currentMatch = true;
  100. somethingChanged = true;
  101. }
  102. }
  103. if (somethingChanged) {
  104. this._onFindReplaceStateChange.fire(changeEvent);
  105. }
  106. }
  107. change(newState, moveCursor, updateHistory = true) {
  108. var _a;
  109. const changeEvent = {
  110. moveCursor: moveCursor,
  111. updateHistory: updateHistory,
  112. searchString: false,
  113. replaceString: false,
  114. isRevealed: false,
  115. isReplaceRevealed: false,
  116. isRegex: false,
  117. wholeWord: false,
  118. matchCase: false,
  119. preserveCase: false,
  120. searchScope: false,
  121. matchesPosition: false,
  122. matchesCount: false,
  123. currentMatch: false,
  124. loop: false,
  125. isSearching: false,
  126. filters: false
  127. };
  128. let somethingChanged = false;
  129. const oldEffectiveIsRegex = this.isRegex;
  130. const oldEffectiveWholeWords = this.wholeWord;
  131. const oldEffectiveMatchCase = this.matchCase;
  132. const oldEffectivePreserveCase = this.preserveCase;
  133. if (typeof newState.searchString !== 'undefined') {
  134. if (this._searchString !== newState.searchString) {
  135. this._searchString = newState.searchString;
  136. changeEvent.searchString = true;
  137. somethingChanged = true;
  138. }
  139. }
  140. if (typeof newState.replaceString !== 'undefined') {
  141. if (this._replaceString !== newState.replaceString) {
  142. this._replaceString = newState.replaceString;
  143. changeEvent.replaceString = true;
  144. somethingChanged = true;
  145. }
  146. }
  147. if (typeof newState.isRevealed !== 'undefined') {
  148. if (this._isRevealed !== newState.isRevealed) {
  149. this._isRevealed = newState.isRevealed;
  150. changeEvent.isRevealed = true;
  151. somethingChanged = true;
  152. }
  153. }
  154. if (typeof newState.isReplaceRevealed !== 'undefined') {
  155. if (this._isReplaceRevealed !== newState.isReplaceRevealed) {
  156. this._isReplaceRevealed = newState.isReplaceRevealed;
  157. changeEvent.isReplaceRevealed = true;
  158. somethingChanged = true;
  159. }
  160. }
  161. if (typeof newState.isRegex !== 'undefined') {
  162. this._isRegex = newState.isRegex;
  163. }
  164. if (typeof newState.wholeWord !== 'undefined') {
  165. this._wholeWord = newState.wholeWord;
  166. }
  167. if (typeof newState.matchCase !== 'undefined') {
  168. this._matchCase = newState.matchCase;
  169. }
  170. if (typeof newState.preserveCase !== 'undefined') {
  171. this._preserveCase = newState.preserveCase;
  172. }
  173. if (typeof newState.searchScope !== 'undefined') {
  174. if (!((_a = newState.searchScope) === null || _a === void 0 ? void 0 : _a.every((newSearchScope) => {
  175. var _a;
  176. return (_a = this._searchScope) === null || _a === void 0 ? void 0 : _a.some(existingSearchScope => {
  177. return !Range.equalsRange(existingSearchScope, newSearchScope);
  178. });
  179. }))) {
  180. this._searchScope = newState.searchScope;
  181. changeEvent.searchScope = true;
  182. somethingChanged = true;
  183. }
  184. }
  185. if (typeof newState.loop !== 'undefined') {
  186. if (this._loop !== newState.loop) {
  187. this._loop = newState.loop;
  188. changeEvent.loop = true;
  189. somethingChanged = true;
  190. }
  191. }
  192. if (typeof newState.isSearching !== 'undefined') {
  193. if (this._isSearching !== newState.isSearching) {
  194. this._isSearching = newState.isSearching;
  195. changeEvent.isSearching = true;
  196. somethingChanged = true;
  197. }
  198. }
  199. if (typeof newState.filters !== 'undefined') {
  200. if (this._filters) {
  201. this._filters.update(newState.filters);
  202. }
  203. else {
  204. this._filters = newState.filters;
  205. }
  206. changeEvent.filters = true;
  207. somethingChanged = true;
  208. }
  209. // Overrides get set when they explicitly come in and get reset anytime something else changes
  210. this._isRegexOverride = (typeof newState.isRegexOverride !== 'undefined' ? newState.isRegexOverride : 0 /* FindOptionOverride.NotSet */);
  211. this._wholeWordOverride = (typeof newState.wholeWordOverride !== 'undefined' ? newState.wholeWordOverride : 0 /* FindOptionOverride.NotSet */);
  212. this._matchCaseOverride = (typeof newState.matchCaseOverride !== 'undefined' ? newState.matchCaseOverride : 0 /* FindOptionOverride.NotSet */);
  213. this._preserveCaseOverride = (typeof newState.preserveCaseOverride !== 'undefined' ? newState.preserveCaseOverride : 0 /* FindOptionOverride.NotSet */);
  214. if (oldEffectiveIsRegex !== this.isRegex) {
  215. somethingChanged = true;
  216. changeEvent.isRegex = true;
  217. }
  218. if (oldEffectiveWholeWords !== this.wholeWord) {
  219. somethingChanged = true;
  220. changeEvent.wholeWord = true;
  221. }
  222. if (oldEffectiveMatchCase !== this.matchCase) {
  223. somethingChanged = true;
  224. changeEvent.matchCase = true;
  225. }
  226. if (oldEffectivePreserveCase !== this.preserveCase) {
  227. somethingChanged = true;
  228. changeEvent.preserveCase = true;
  229. }
  230. if (somethingChanged) {
  231. this._onFindReplaceStateChange.fire(changeEvent);
  232. }
  233. }
  234. canNavigateBack() {
  235. return this.canNavigateInLoop() || (this.matchesPosition !== 1);
  236. }
  237. canNavigateForward() {
  238. return this.canNavigateInLoop() || (this.matchesPosition < this.matchesCount);
  239. }
  240. canNavigateInLoop() {
  241. return this._loop || (this.matchesCount >= MATCHES_LIMIT);
  242. }
  243. }