Skip to content

Migrate regex modifiers to scope handlers #1570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ import ContainingSyntaxScopeStage, {
SimpleEveryScopeModifier,
} from "./modifiers/scopeTypeStages/ContainingSyntaxScopeStage";
import NotebookCellStage from "./modifiers/scopeTypeStages/NotebookCellStage";
import {
CustomRegexModifier,
CustomRegexStage,
NonWhitespaceSequenceStage,
UrlStage,
} from "./modifiers/scopeTypeStages/RegexStage";
import { StoredTargetMap } from "..";

export class ModifierStageFactoryImpl implements ModifierStageFactory {
Expand Down Expand Up @@ -131,20 +125,14 @@ export class ModifierStageFactoryImpl implements ModifierStageFactory {
switch (modifier.scopeType.type) {
case "notebookCell":
return new NotebookCellStage(modifier);
case "nonWhitespaceSequence":
return new NonWhitespaceSequenceStage(modifier);
case "boundedNonWhitespaceSequence":
return new BoundedNonWhitespaceSequenceStage(
this.languageDefinitions,
this,
modifier,
);
case "url":
return new UrlStage(modifier);
case "collectionItem":
return new ItemStage(this.languageDefinitions, modifier);
case "customRegex":
return new CustomRegexStage(modifier as CustomRegexModifier);
case "surroundingPair":
return new SurroundingPairStage(
this.languageDefinitions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { CustomRegexScopeType, Direction, ScopeType } from "@cursorless/common";
import { imap } from "itertools";
import { NestedScopeHandler, ScopeHandlerFactory } from ".";
import { generateMatchesInRange } from "../../../util/getMatchesInRange";
import { TokenTarget } from "../../targets";
import { TargetScope } from "./scope.types";

abstract class RegexStageBase extends NestedScopeHandler {
public readonly iterationScopeType: ScopeType = { type: "line" };
protected abstract readonly regex: RegExp;

protected generateScopesInSearchScope(
direction: Direction,
{ editor, domain }: TargetScope,
): Iterable<TargetScope> {
return imap(
generateMatchesInRange(this.regex, editor, domain, direction),
(range) => ({
editor,
domain: range,
getTargets: (isReversed) => [
new TokenTarget({
editor,
contentRange: range,
isReversed,
}),
],
}),
);
}
}

export class NonWhitespaceSequenceScopeHandler extends RegexStageBase {
regex = /\S+/g;
}

export class UrlScopeHandler extends RegexStageBase {
// taken from https://regexr.com/3e6m0
regex =
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;

constructor(
scopeHandlerFactory: ScopeHandlerFactory,
readonly scopeType: ScopeType,
languageId: string,
) {
super(scopeHandlerFactory, scopeType, languageId);
}
}

export class CustomRegexScopeHandler extends RegexStageBase {
get regex() {
return new RegExp(this.scopeType.regex, "gu");
}

constructor(
scopeHandlerFactory: ScopeHandlerFactory,
readonly scopeType: CustomRegexScopeType,
languageId: string,
) {
super(scopeHandlerFactory, scopeType, languageId);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import type { ScopeType } from "@cursorless/common";
import {
CharacterScopeHandler,
CustomRegexScopeHandler,
DocumentScopeHandler,
IdentifierScopeHandler,
LineScopeHandler,
NonWhitespaceSequenceScopeHandler,
OneOfScopeHandler,
ParagraphScopeHandler,
ScopeHandlerFactory,
TokenScopeHandler,
UrlScopeHandler,
WordScopeHandler,
} from ".";
import { LanguageDefinitions } from "../../../languages/LanguageDefinitions";
import { ScopeHandlerFactory } from "./ScopeHandlerFactory";
import type { CustomScopeType, ScopeHandler } from "./scopeHandler.types";

/**
Expand Down Expand Up @@ -56,6 +59,16 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
return OneOfScopeHandler.create(this, scopeType, languageId);
case "paragraph":
return new ParagraphScopeHandler(scopeType, languageId);
case "nonWhitespaceSequence":
return new NonWhitespaceSequenceScopeHandler(
this,
scopeType,
languageId,
);
case "url":
return new UrlScopeHandler(this, scopeType, languageId);
case "customRegex":
return new CustomRegexScopeHandler(this, scopeType, languageId);
case "custom":
return scopeType.scopeHandler;
case "instance":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export * from "./OneOfScopeHandler";
export { default as OneOfScopeHandler } from "./OneOfScopeHandler";
export * from "./ParagraphScopeHandler";
export { default as ParagraphScopeHandler } from "./ParagraphScopeHandler";
export * from "./RegexScopeHandler";
export * from "./ScopeHandlerFactory";
export * from "./ScopeHandlerFactoryImpl";

This file was deleted.