forked from cursorless-dev/cursorless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelimiterMaps.ts
More file actions
101 lines (92 loc) · 2.85 KB
/
delimiterMaps.ts
File metadata and controls
101 lines (92 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {
ComplexSurroundingPairName,
SimpleSurroundingPairName,
} from "@cursorless/common";
import { unsafeKeys } from "../../../util/object";
type IndividualDelimiterText = string | string[];
type DelimiterMap = Record<
SimpleSurroundingPairName,
[IndividualDelimiterText, IndividualDelimiterText]
>;
const delimiterToText: DelimiterMap = Object.freeze({
angleBrackets: [
["</", "<"],
[">", "/>"],
],
backtickQuotes: ["`", "`"],
curlyBrackets: [["{", "${"], "}"],
doubleQuotes: ['"', '"'],
escapedDoubleQuotes: ['\\"', '\\"'],
escapedParentheses: ["\\(", "\\)"],
escapedSquareBrackets: ["\\[", "\\]"],
escapedSingleQuotes: ["\\'", "\\'"],
parentheses: [["(", "$("], ")"],
singleQuotes: ["'", "'"],
squareBrackets: ["[", "]"],
});
// FIXME: Probably remove these as part of
// https://github.com/cursorless-dev/cursorless/issues/1812#issuecomment-1691493746
const delimiterToTextOverrides: Record<string, Partial<DelimiterMap>> = {
nix: {
singleQuotes: ["''", "''"],
},
lua: {
// FIXME: Add special double square brackets
// see https://github.com/cursorless-dev/cursorless/pull/2012#issuecomment-1808214409
// see also https://github.com/cursorless-dev/cursorless/issues/1812#issuecomment-1691493746
doubleQuotes: [
['"', "[["],
['"', "]]"],
],
},
};
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: unsafeKeys(delimiterToText),
string: ["singleQuotes", "doubleQuotes", "backtickQuotes"],
collectionBoundary: [
"parentheses",
"squareBrackets",
"curlyBrackets",
"angleBrackets",
],
};
/**
* Given a language id, returns a list of all possible delimiters for that
* language.
*
* Allows us to support languages where the parse tree gives type names to nodes
* that don't correspond to the actual delimiter.
*
* Note that we pass in `undefined` if we are in a text fragment, because then
* we won't be using a parse tree.
*
* FIXME: Probably remove these as part of
* https://github.com/cursorless-dev/cursorless/issues/1812#issuecomment-1691493746
*
* @param languageId The language id, or `undefined` if in a text fragment
* @returns A list of all possible delimiters for that language
*/
export function getSimpleDelimiterMap(
languageId: string | undefined,
): Record<
SimpleSurroundingPairName,
[IndividualDelimiterText, IndividualDelimiterText]
> {
if (languageId != null && languageId in delimiterToTextOverrides) {
return {
...delimiterToText,
...delimiterToTextOverrides[languageId],
};
}
return delimiterToText;
}