Skip to content

Migrating scope stages into handlers #1073

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/actions/GenerateSnippet/GenerateSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Offsets } from "../../processTargets/modifiers/surroundingPair/types";
import isTesting from "../../testUtil/isTesting";
import { Target } from "../../typings/target.types";
import { Graph } from "../../typings/Types";
import { getDocumentRange } from "../../util/range";
import { getDocumentRange } from "../../util/rangeUtils";
import { selectionFromRange } from "../../util/selectionUtils";
import { Action, ActionReturnValue } from "../actions.types";
import { constructSnippetBody } from "./constructSnippetBody";
Expand Down
3 changes: 0 additions & 3 deletions src/processTargets/modifiers/getLegacyScopeStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import ContainingSyntaxScopeStage, {
SimpleContainingScopeModifier,
SimpleEveryScopeModifier,
} from "./scopeTypeStages/ContainingSyntaxScopeStage";
import DocumentStage from "./scopeTypeStages/DocumentStage";
import NotebookCellStage from "./scopeTypeStages/NotebookCellStage";
import ParagraphStage from "./scopeTypeStages/ParagraphStage";
import {
Expand Down Expand Up @@ -40,8 +39,6 @@ export default function getLegacyScopeStage(
switch (modifier.scopeType.type) {
case "notebookCell":
return new NotebookCellStage(modifier);
case "document":
return new DocumentStage(modifier);
case "paragraph":
return new ParagraphStage(modifier);
case "nonWhitespaceSequence":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Position, Range, TextEditor } from "vscode";
import { Direction, ScopeType } from "../../../typings/targetDescriptor.types";
import { getDocumentRange } from "../../../util/rangeUtils";
import { DocumentTarget } from "../../targets";
import { OutOfRangeError } from "../targetSequenceUtils";
import NotHierarchicalScopeError from "./NotHierarchicalScopeError";
import { IterationScope, TargetScope } from "./scope.types";
import { ScopeHandler } from "./scopeHandler.types";

export default class DocumentScopeHandler implements ScopeHandler {
iterationScopeType = undefined;

constructor(public scopeType: ScopeType, private languageId: string) {}

getScopesTouchingPosition(
editor: TextEditor,
position: Position,
ancestorIndex: number = 0
): TargetScope[] {
if (ancestorIndex !== 0) {
throw new NotHierarchicalScopeError(this.scopeType);
}

return this.getIterationScope(editor).getScopes();
}

getScopesOverlappingRange(editor: TextEditor, _range: Range): TargetScope[] {
return this.getIterationScope(editor).getScopes();
}

getIterationScopesTouchingPosition(
editor: TextEditor,
_position: Position
): IterationScope[] {
return [this.getIterationScope(editor)];
}

getScopeRelativeToPosition(
editor: TextEditor,
position: Position,
offset: number,
_direction: Direction
): TargetScope {
if (offset === 0) {
return this.getIterationScope(editor).getScopes()[0];
}

throw new OutOfRangeError();
}

private getIterationScope(editor: TextEditor): IterationScope {
const contentRange = getDocumentRange(editor.document);

return {
editor,
domain: contentRange,
getScopes: () => [
{
editor,
domain: contentRange,
getTarget: (isReversed) =>
new DocumentTarget({
editor,
isReversed,
contentRange,
}),
},
],
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { range } from "lodash";
import { Position, Range, TextEditor } from "vscode";
import { Direction } from "../../../typings/targetDescriptor.types";
import { getDocumentRange } from "../../../util/range";
import { Direction, ScopeType } from "../../../typings/targetDescriptor.types";
import { getDocumentRange } from "../../../util/rangeUtils";
import { LineTarget } from "../../targets";
import { OutOfRangeError } from "../targetSequenceUtils";
import NotHierarchicalScopeError from "./NotHierarchicalScopeError";
Expand All @@ -12,7 +12,7 @@ export default class LineScopeHandler implements ScopeHandler {
public readonly iterationScopeType = { type: "document" } as const;

constructor(
public readonly scopeType: { type: "line" },
public readonly scopeType: ScopeType,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of using this type information for generating our docs. Do you mind if we leave it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not turn it into a proper type instead? I mean in the types file we have made them all SimpleScopeType if we actually want to have it more fine grain I think we should do it at that end so we don't have to do casts when calling the constructor.
public readonly scopeType: LineScopeType

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would giving it a name allow us to avoid casts?

protected languageId: string
) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
CharacterScopeHandler,
DocumentScopeHandler,
IdentifierScopeHandler,
LineScopeHandler,
TokenScopeHandler,
Expand Down Expand Up @@ -39,7 +40,9 @@ export default function getScopeHandler(
case "identifier":
return new IdentifierScopeHandler(scopeType, languageId);
case "line":
return new LineScopeHandler(scopeType as { type: "line" }, languageId);
return new LineScopeHandler(scopeType, languageId);
case "document":
return new DocumentScopeHandler(scopeType, languageId);
default:
return undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions src/processTargets/modifiers/scopeHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ export * from "./WordScopeHandler";
export { default as WordScopeHandler } from "./WordScopeHandler";
export * from "./TokenScopeHandler";
export { default as TokenScopeHandler } from "./TokenScopeHandler";
export * from "./DocumentScopeHandler";
export { default as DocumentScopeHandler } from "./DocumentScopeHandler";
export * from "./getScopeHandler";
export { default as getScopeHandler } from "./getScopeHandler";
30 changes: 0 additions & 30 deletions src/processTargets/modifiers/scopeTypeStages/DocumentStage.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/testUtil/TestCaseRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import HatTokenMap from "../core/HatTokenMap";
import { injectSpyIde, SpyInfo } from "../ide/spies/SpyIDE";
import { DecoratedSymbolMark } from "../typings/targetDescriptor.types";
import { Graph } from "../typings/Types";
import { getDocumentRange } from "../util/range";
import { getDocumentRange } from "../util/rangeUtils";
import sleep from "../util/sleep";
import { extractTargetedMarks } from "./extractTargetedMarks";
import serialize from "./serialize";
Expand Down
14 changes: 0 additions & 14 deletions src/util/range.ts

This file was deleted.

15 changes: 14 additions & 1 deletion src/util/rangeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Position, Range, TextEditor } from "vscode";
import { Position, Range, TextDocument, TextEditor } from "vscode";

export function isAtEndOfLine(editor: TextEditor, position: Position) {
const endLine = editor.document.lineAt(position);
Expand Down Expand Up @@ -50,3 +50,16 @@ export function getRangeLength(editor: TextEditor, range: Range) {
export function strictlyContains(range1: Range, range2: Range): boolean {
return range1.start.isBefore(range2.start) && range1.end.isAfter(range2.end);
}

/**
* Get a range that corresponds to the entire contents of the given document.
*
* @param document The document to consider
* @returns A range corresponding to the entire document contents
*/
export function getDocumentRange(document: TextDocument) {
return new Range(
new Position(0, 0),
document.lineAt(document.lineCount - 1).range.end
);
}