Skip to content

Commit

Permalink
Add language overrides for delimiter maps (#2012)
Browse files Browse the repository at this point in the history
This is me breaking out some functionality that was part of PR #1911
since that PR has lots of work to be done and it may still benefit other
languages that get in sooner. I've applied the suggested changes from
pokey in that PR's code review.

I've also added a placeholder map for lua which uses `[[ ]]` for
multiline support as noted in PR #1962, however I realized as I went to
add it that it's not as simple as the way I did it for nix because I
can't just reuse the existing `singleQuote` entry since lua actually
uses single quotes. So this may actually be a bit harder, and reminds me
of [this
discussion](#1992 (comment)).
So before I go randomly hacking stuff I'm curious what you think the
best approach here is.

These are the comments I had left in the Nix PR about these changes:

* I called returning delimiterToText as getSimpleDelimiterMap to kind of
mirror complexDelimiterMap.

* I'm not sure is if you still want a delimiterMap.ts standalone file,
and then a getSimpleDelimiterMap.ts only for that function. Now
delimiterToText isn't referenced anywhere else, so seemed maybe okay to
keep them together.

* I also thought about adding getComplexDelimiterMap(), but atm because
complexDelimiterMap only ever uses the keys from the delimiterToText it
won't change even if the language has different values, so seemed
unnecessary. These are all things I could guess at your preferences, but
may as well ask instead.

* I also noticed leftToRightMap in that file isn't actually used
anywhere so can be deleted I think, but also not sure about doing that
as part of a totally unrelated code change, to keep commits clean.


## Checklist

- [x] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [-] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [x] I have not broken the cheatsheet

---------

Co-authored-by: fidgetingbits <fidgetingbits@memeoid.cx>
Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 13, 2024
1 parent 2d07d11 commit 1abf38d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
23 changes: 23 additions & 0 deletions data/fixtures/recorded/languages/lua/changeString2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: lua
command:
version: 7
spokenForm: change string
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: surroundingPair, delimiter: string}
usePrePhraseSnapshot: true
initialState:
documentContents: "[[aaa]]"
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
marks: {}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { unsafeKeys } from "../../../util/object";

type IndividualDelimiterText = string | string[];

export const delimiterToText: Record<
type DelimiterMap = Record<
SimpleSurroundingPairName,
[IndividualDelimiterText, IndividualDelimiterText]
> = Object.freeze({
>;

const delimiterToText: DelimiterMap = Object.freeze({
angleBrackets: [
["</", "<"],
[">", "/>"],
Expand All @@ -26,6 +28,24 @@ export const delimiterToText: Record<
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),
);
Expand All @@ -47,3 +67,35 @@ export const complexDelimiterMap: Record<
"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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export function findSurroundingPairParseTreeBased(
) {
const document: TextDocument = editor.document;

const individualDelimiters = getIndividualDelimiters(delimiters);
const individualDelimiters = getIndividualDelimiters(
document.languageId,
delimiters,
);

const delimiterTextToDelimiterInfoMap = Object.fromEntries(
individualDelimiters.map((individualDelimiter) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function findSurroundingPairTextBased(
const document: TextDocument = editor.document;
const fullRange = allowableRange ?? document.range;

const individualDelimiters = getIndividualDelimiters(delimiters);
const individualDelimiters = getIndividualDelimiters(undefined, delimiters);

const delimiterTextToDelimiterInfoMap = Object.fromEntries(
individualDelimiters.map((individualDelimiter) => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { SimpleSurroundingPairName, isString } from "@cursorless/common";
import { IndividualDelimiter } from "./types";
import { delimiterToText } from "./delimiterMaps";
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
* @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];

Expand Down

0 comments on commit 1abf38d

Please sign in to comment.