forked from cursorless-dev/cursorless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetIndividualDelimiters.ts
More file actions
53 lines (48 loc) · 1.83 KB
/
getIndividualDelimiters.ts
File metadata and controls
53 lines (48 loc) · 1.83 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
import { SimpleSurroundingPairName, isString } from "@cursorless/common";
import { IndividualDelimiter } from "./types";
import { getSimpleDelimiterMap } from "./delimiterMaps";
import { concat, uniq } from "lodash";
/**
* Given a list of delimiters, returns a list where each element corresponds to
* a single right or left delimiter. Each item contains information such as a
* reference to delimiter name, the text to expect, etc.
*
* @param languageId The language id, or `undefined` if in a text fragment
* @param delimiters The delimiter names
* @returns A list of information about all possible left / right delimiter
* instances
*/
export function getIndividualDelimiters(
languageId: string | undefined,
delimiters: SimpleSurroundingPairName[],
): IndividualDelimiter[] {
const delimiterToText = getSimpleDelimiterMap(languageId);
return delimiters.flatMap((delimiter) => {
const [leftDelimiter, rightDelimiter] = delimiterToText[delimiter];
// Allow for the fact that a delimiter might have multiple ways to indicate
// its opening / closing
const leftDelimiters = isString(leftDelimiter)
? [leftDelimiter]
: leftDelimiter;
const rightDelimiters = isString(rightDelimiter)
? [rightDelimiter]
: rightDelimiter;
const allDelimiterTexts = uniq(concat(leftDelimiters, rightDelimiters));
return allDelimiterTexts.map((text) => {
const isLeft = leftDelimiters.includes(text);
const isRight = rightDelimiters.includes(text);
return {
text,
// If delimiter text is the same for left and right, we say it's side
// is "unknown", so must be determined from context.
side:
isLeft && !isRight
? "left"
: isRight && !isLeft
? "right"
: "unknown",
delimiter,
};
});
});
}