Skip to content

Textual paired delimiters #324

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 69 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
675f0fe
Initial working parse-tree based matcher
pokey Oct 13, 2021
d792df2
Add some todos
pokey Oct 13, 2021
5b6ad6f
More progress
pokey Oct 20, 2021
35ce354
More work
pokey Oct 27, 2021
d7f3a9b
More progress
pokey Nov 3, 2021
20a192c
Make more progress
pokey Nov 5, 2021
d0fa2b6
More stuff
pokey Nov 11, 2021
ef76ca8
Add comment
pokey Nov 11, 2021
0662487
Another new approach
pokey Nov 12, 2021
9e53354
Initial working version when cursor is on a delimiter
pokey Nov 15, 2021
bc7ecf5
Fix for quad; more tests
pokey Nov 15, 2021
4b51a3a
An attempt at internal ranges
pokey Nov 16, 2021
809db0e
More work
pokey Nov 16, 2021
ef9e868
Add more tests
pokey Nov 16, 2021
227d659
New approach
pokey Nov 17, 2021
4491964
Many more tests
pokey Nov 17, 2021
e924f7c
Test utils
pokey Nov 18, 2021
7364a54
Update test inputs
pokey Nov 18, 2021
385cfa7
Narrow to string contents
pokey Nov 18, 2021
5b5fc96
New tests
pokey Nov 18, 2021
625ff6a
Revert "Update test inputs"
pokey Nov 18, 2021
46c6259
Fix transform
pokey Nov 18, 2021
7554f91
Update test inputs
pokey Nov 18, 2021
aaba0c3
Update test fixture outputs
pokey Nov 18, 2021
b73a07e
Add multiple cursor test
pokey Nov 18, 2021
cdb5909
Adds some chuck tests
pokey Nov 18, 2021
a89f094
Fix java string content range
pokey Nov 18, 2021
ae1f100
Fix green double test
pokey Nov 18, 2021
8a1b839
Another attempt to fix green double test
pokey Nov 18, 2021
b91c35a
Add parse tree parity tests
pokey Nov 18, 2021
6922d85
Update comment
pokey Nov 20, 2021
5b3ea48
Use hack to fix java
pokey Nov 20, 2021
f2b8970
Move a bunch of stuff around
pokey Nov 20, 2021
015de3c
Initial attempt at generalization
pokey Nov 21, 2021
ddf8563
Refactor in preparation for unification
pokey Nov 21, 2021
e55c89a
More refactoring
pokey Nov 21, 2021
a2b2cea
Basically working unified approach
pokey Nov 21, 2021
9f8ff68
Fix tests
pokey Nov 21, 2021
15a6082
Fix incorrect test
pokey Nov 21, 2021
9feaf1c
Fix quote delimiter side identification
pokey Nov 22, 2021
74944bf
Add a bunch of string interpolation tests
pokey Nov 22, 2021
a3f631f
One more interpolation test
pokey Nov 22, 2021
884aacc
One more string interpolation test
pokey Nov 22, 2021
b6ed70a
Attempt to have list of delimiters at every occurrence
pokey Nov 22, 2021
ce9ea0a
Revert "Attempt to have list of delimiters at every occurrence"
pokey Nov 22, 2021
66e0c39
Working string interpolation variables
pokey Nov 22, 2021
04b8c74
Get string working
pokey Nov 23, 2021
cd9b982
Add bash script support
pokey Nov 23, 2021
968e1f5
Fixed tests
pokey Nov 23, 2021
b152323
Organize surrounding pair tests by language id
pokey Nov 23, 2021
a9a148c
Cleanup
pokey Nov 23, 2021
a027741
Cleanup and add lots of docstrings
pokey Nov 23, 2021
1ffd03b
More docstring
pokey Nov 23, 2021
989f770
More cleanup
pokey Nov 23, 2021
7ee2e7f
More cleanup
pokey Nov 23, 2021
f696967
More docstrings
pokey Nov 24, 2021
f692e1f
Better handle large documents
pokey Nov 24, 2021
90bbc27
More dock strings
pokey Nov 24, 2021
7b22c38
More dock
pokey Nov 24, 2021
d5e9e40
More cleanup
pokey Nov 24, 2021
5afb332
More cleanup
pokey Nov 24, 2021
00ca167
docstring
pokey Nov 24, 2021
03dec94
Switch to new conventions
pokey Nov 24, 2021
21129cf
More comments
pokey Nov 24, 2021
f753248
Fix bug with greater-than vs angle; add `$(` to round
pokey Nov 25, 2021
8f7c636
Initial working parity-based quote disambiguation
pokey Nov 25, 2021
325fddd
Clean up inferDelimiterSide implementation
pokey Nov 25, 2021
82987af
Support forcing quote direction
pokey Nov 26, 2021
32ae8d5
Merge branch 'main' into textual-paired-delimiters
AndreasArvidsson Nov 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion src/core/tokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { matchAll } from "../util/regex";

const REPEATABLE_SYMBOLS = [
"+",
"-",
Expand Down Expand Up @@ -55,7 +57,7 @@ export function tokenize<T>(
text: string,
mapfn: (v: RegExpMatchArray, k: number) => T
) {
return Array.from(text.matchAll(TOKEN_MATCHER), mapfn);
return matchAll(text, TOKEN_MATCHER, mapfn);
}

//https://stackoverflow.com/a/6969486
Expand Down
8 changes: 8 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class UnsupportedLanguageError extends Error {
constructor(languageId: string) {
super(
`Language '${languageId}' is not implemented yet; See https://github.com/pokey/cursorless-vscode/blob/main/docs/adding-a-new-language.md`
);
this.name = "UnsupportedLanguageError";
}
}
5 changes: 2 additions & 3 deletions src/languages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java from "./java";
import json from "./json";
import python from "./python";
import typescript from "./typescript";
import { UnsupportedLanguageError } from "../errors";

const languageMatchers: Record<string, Record<ScopeType, NodeMatcher>> = {
c: cpp,
Expand All @@ -36,9 +37,7 @@ export function getNodeMatcher(
const matchers = languageMatchers[languageId];

if (matchers == null) {
throw Error(
`Language '${languageId}' is not implemented yet; See https://github.com/pokey/cursorless-vscode/blob/main/docs/adding-a-new-language.md`
);
throw new UnsupportedLanguageError(languageId);
}

const matcher = matchers[scopeType];
Expand Down
126 changes: 0 additions & 126 deletions src/languages/surroundingPair.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/processTargets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TypedSelection,
} from "../typings/Types";
import processMark from "./processMark";
import processModifier from "./processModifier";
import processModifier from "./modifiers/processModifier";
import processPosition from "./processPosition";
import processSelectionType from "./processSelectionType";
import { isForward as getIsForward } from "../util/selectionUtils";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import update from "immutability-helper";
import { range } from "lodash";
import { Location, Position, Range, Selection } from "vscode";
import { SyntaxNode } from "web-tree-sitter";
import { SUBWORD_MATCHER } from "../core/constants";
import { getNodeMatcher } from "../languages";
import { createSurroundingPairMatcher } from "../languages/surroundingPair";
import { selectionWithEditorFromRange } from "../util/selectionUtils";
import { SUBWORD_MATCHER } from "../../core/constants";
import { getNodeMatcher } from "../../languages";
import { selectionWithEditorFromRange } from "../../util/selectionUtils";
import {
ContainingScopeModifier,
HeadModifier,
Expand All @@ -15,11 +14,11 @@ import {
SelectionContext,
SelectionWithEditor,
SubTokenModifier,
SurroundingPairModifier,
TailModifier,
} from "../typings/Types";
} from "../../typings/Types";
import { processSurroundingPair } from "./surroundingPair";

type SelectionWithContext = {
export type SelectionWithEditorWithContext = {
selection: SelectionWithEditor;
context: SelectionContext;
};
Expand All @@ -28,7 +27,7 @@ export default function (
context: ProcessedTargetsContext,
target: PrimitiveTarget,
selection: SelectionWithEditor
): SelectionWithContext[] {
): SelectionWithEditorWithContext[] {
const { modifier } = target;
let result;

Expand Down Expand Up @@ -66,7 +65,7 @@ function processScopeType(
context: ProcessedTargetsContext,
selection: SelectionWithEditor,
modifier: ContainingScopeModifier
): SelectionWithContext[] | null {
): SelectionWithEditorWithContext[] | null {
const nodeMatcher = getNodeMatcher(
selection.editor.document.languageId,
modifier.scopeType,
Expand All @@ -93,7 +92,7 @@ function processSubToken(
context: ProcessedTargetsContext,
selection: SelectionWithEditor,
modifier: SubTokenModifier
): SelectionWithContext[] | null {
): SelectionWithEditorWithContext[] | null {
const token = selection.editor.document.getText(selection.selection);
let pieces: { start: number; end: number }[] = [];

Expand Down Expand Up @@ -190,7 +189,7 @@ function processHeadTail(
context: ProcessedTargetsContext,
selection: SelectionWithEditor,
modifier: HeadModifier | TailModifier
): SelectionWithContext[] | null {
): SelectionWithEditorWithContext[] | null {
let anchor: Position, active: Position;
if (modifier.type === "head") {
anchor = selection.selection.end;
Expand All @@ -210,22 +209,7 @@ function processHeadTail(
];
}

function processSurroundingPair(
context: ProcessedTargetsContext,
selection: SelectionWithEditor,
modifier: SurroundingPairModifier
): SelectionWithContext[] | null {
const node: SyntaxNode | null = context.getNodeAtLocation(
new Location(selection.editor.document.uri, selection.selection)
);
const nodeMatcher = createSurroundingPairMatcher(
modifier.delimiter,
modifier.delimitersOnly
);
return findNearestContainingAncestorNode(node, nodeMatcher, selection);
}

function findNearestContainingAncestorNode(
export function findNearestContainingAncestorNode(
startNode: SyntaxNode,
nodeMatcher: NodeMatcher,
selection: SelectionWithEditor
Expand Down
14 changes: 14 additions & 0 deletions src/processTargets/modifiers/surroundingPair/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// XXX: Make this language-specific if it starts to get unwieldy
/**
* A list of node types that are allowed to be parents of an angle bracket.
* Having this list allows us to avoid trying to treat a greater-than sign as an
* angle bracket
*/
export const ALLOWABLE_ANGLE_BRACKET_PARENTS = [
"end_tag",
"jsx_closing_element",
"jsx_opening_element",
"jsx_self_closing_element",
"self_closing_tag",
"start_tag",
];
38 changes: 38 additions & 0 deletions src/processTargets/modifiers/surroundingPair/delimiterMaps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
ComplexSurroundingPairName,
SimpleSurroundingPairName,
} from "../../../typings/Types";

type IndividualDelimiterText = string | string[];

export const delimiterToText: Record<
SimpleSurroundingPairName,
[IndividualDelimiterText, IndividualDelimiterText]
> = {
angleBrackets: ["<", ">"],
backtickQuotes: ["`", "`"],
curlyBrackets: [["{", "${"], "}"],
doubleQuotes: ['"', '"'],
escapedDoubleQuotes: ['\\"', '\\"'],
escapedParentheses: ["\\(", "\\)"],
escapedSingleQuotes: ["\\'", "\\'"],
parentheses: [["(", "$("], ")"],
singleQuotes: ["'", "'"],
squareBrackets: ["[", "]"],
};

export const leftToRightMap: Record<string, string> = Object.fromEntries(
Object.values(delimiterToText)
);

/**
* Some surrounding pair scope types are really just shorthand for multiple
* acceptable delimiters. This map defines these surrounding pairs.
*/
export const complexDelimiterMap: Record<
ComplexSurroundingPairName,
SimpleSurroundingPairName[]
> = {
any: Object.keys(delimiterToText),
string: ["singleQuotes", "doubleQuotes", "backtickQuotes"],
};
Loading