findState.js 9.1 KB

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