-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/cursorless-vscode/src/revisualizeOnCustomRegexChange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { | ||
Disposable, | ||
ScopeProvider, | ||
ScopeTypeInfo, | ||
disposableFrom, | ||
} from "@cursorless/common"; | ||
import { isEqual } from "lodash"; | ||
import { | ||
ScopeVisualizer, | ||
VisualizationType, | ||
} from "./ScopeVisualizerCommandApi"; | ||
|
||
/** | ||
* Attempts to ensure that the scope visualizer is still visualizing the same | ||
* scope type after the user changes one of their custom regexes. Because custom | ||
* regexes don't have a unique identifier, we have to do some guesswork to | ||
* figure out which custom regex the user changed. This function look for a | ||
* custom regex with the same spoken form as the one that was changed, and if it | ||
* finds one, it starts visualizing that one instead. | ||
* | ||
* @param scopeVisualizer The scope visualizer to listen to | ||
* @param scopeProvider Provides scope information | ||
* @returns A {@link Disposable} which will stop the callback from running | ||
*/ | ||
export function revisualizeOnCustomRegexChange( | ||
scopeVisualizer: ScopeVisualizer, | ||
scopeProvider: ScopeProvider, | ||
): Disposable { | ||
let currentRegexScopeInfo: ScopeTypeInfo | undefined; | ||
let currentVisualizationType: VisualizationType | undefined; | ||
|
||
return disposableFrom( | ||
scopeVisualizer.onDidChangeScopeType((scopeType, visualizationType) => { | ||
currentRegexScopeInfo = | ||
scopeType?.type === "customRegex" | ||
? scopeProvider.getScopeInfo(scopeType) | ||
: undefined; | ||
currentVisualizationType = visualizationType; | ||
}), | ||
|
||
scopeProvider.onDidChangeScopeInfo((scopeInfos) => { | ||
if ( | ||
currentRegexScopeInfo != null && | ||
!scopeInfos.some((scopeInfo) => | ||
isEqual(scopeInfo.scopeType, currentRegexScopeInfo!.scopeType), | ||
) | ||
) { | ||
const replacement = scopeInfos.find( | ||
(scopeInfo) => | ||
scopeInfo.scopeType.type === "customRegex" && | ||
isEqual(scopeInfo.spokenForm, currentRegexScopeInfo!.spokenForm), | ||
); | ||
if (replacement != null) { | ||
scopeVisualizer.start( | ||
replacement.scopeType, | ||
currentVisualizationType!, | ||
); | ||
} | ||
} | ||
}), | ||
); | ||
} |