Skip to content

Add support for domain, leading, trailing, interior #1427

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
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
4 changes: 2 additions & 2 deletions packages/cursorless-engine/src/cursorlessEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MarkStageFactoryImpl } from "./processTargets/MarkStageFactoryImpl";
import { ModifierStageFactoryImpl } from "./processTargets/ModifierStageFactoryImpl";
import { ScopeHandlerFactoryImpl } from "./processTargets/modifiers/scopeHandlers";
import { injectIde } from "./singletons/ide.singleton";
import { LanguageDefinitionsImpl } from "./languages/LanguageDefinitionsImpl";
import { LanguageDefinitions } from "./languages/LanguageDefinitions";

export function createCursorlessEngine(
treeSitter: TreeSitter,
Expand Down Expand Up @@ -37,7 +37,7 @@ export function createCursorlessEngine(

const testCaseRecorder = new TestCaseRecorder(hatTokenMap);

const languageDefinitions = new LanguageDefinitionsImpl(treeSitter);
const languageDefinitions = new LanguageDefinitions(treeSitter);
const scopeHandlerFactory = new ScopeHandlerFactoryImpl(languageDefinitions);
const markStageFactory = new MarkStageFactoryImpl();
const modifierStageFactory = new ModifierStageFactoryImpl(
Expand Down
69 changes: 69 additions & 0 deletions packages/cursorless-engine/src/languages/LanguageDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ScopeType, SimpleScopeType } from "@cursorless/common";
import { Query } from "web-tree-sitter";
import { ide } from "../singletons/ide.singleton";
import { join } from "path";
import { TreeSitterScopeHandler } from "../processTargets/modifiers/scopeHandlers";
import { TreeSitter } from "../typings/TreeSitter";
import { existsSync, readFileSync } from "fs";
import { LanguageId } from "./constants";

/**
* Represents a language definition for a single language, including the
* tree-sitter query used to extract scopes for the given language
*/
export class LanguageDefinition {
private constructor(
private treeSitter: TreeSitter,
/**
* The tree-sitter query used to extract scopes for the given language.
* Note that this query contains patterns for all scope types that the
* language supports using new-style tree-sitter queries
*/
private query: Query,
) {}

/**
* Construct a language definition for the given language id, if the language
* has a new-style query definition, or return undefined if the language doesn't
*
* @param treeSitter The tree-sitter instance to use for parsing
* @param languageId The language id for which to create a language definition
* @returns A language definition for the given language id, or undefined if the given language
* id doesn't have a new-style query definition
*/
static create(
treeSitter: TreeSitter,
languageId: LanguageId,
): LanguageDefinition | undefined {
const queryPath = join(ide().assetsRoot, "queries", `${languageId}.scm`);

if (!existsSync(queryPath)) {
return undefined;
}

const rawLanguageQueryString = readFileSync(queryPath, "utf8");

return new LanguageDefinition(
treeSitter,
treeSitter.getLanguage(languageId)!.query(rawLanguageQueryString),
);
}

/**
* @param scopeType The scope type for which to get a scope handler
* @returns A scope handler for the given scope type and language id, or
* undefined if the given scope type / language id combination is still using
* legacy pathways
*/
getScopeHandler(scopeType: ScopeType) {
if (!this.query.captureNames.includes(scopeType.type)) {
return undefined;
}

return new TreeSitterScopeHandler(
this.treeSitter,
this.query,
scopeType as SimpleScopeType,
);
}
}
38 changes: 0 additions & 38 deletions packages/cursorless-engine/src/languages/LanguageDefinitionImpl.ts

This file was deleted.

60 changes: 51 additions & 9 deletions packages/cursorless-engine/src/languages/LanguageDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
import { ScopeType } from "@cursorless/common";
import { ScopeHandler } from "../processTargets/modifiers/scopeHandlers/scopeHandler.types";
import { TreeSitter } from "..";
import { LanguageDefinition } from "./LanguageDefinition";
import { LanguageId } from "./constants";

export interface LanguageDefinitions {
get(languageId: string): LanguageDefinition | undefined;
}
/**
* Sentinel value to indicate that a language doesn't have
* a new-style query definition file
*/
const LANGUAGE_UNDEFINED = Symbol("LANGUAGE_UNDEFINED");

/**
* Keeps a map from language ids to {@link LanguageDefinition} instances,
* constructing them as necessary
*/
export class LanguageDefinitions {
/**
* Maps from language id to {@link LanguageDefinition} or
* {@link LANGUAGE_UNDEFINED} if language doesn't have new-style definitions.
* We use a sentinel value instead of undefined so that we can distinguish
* between a situation where we haven't yet checked whether a language has a
* new-style query definition and a situation where we've checked and found
* that it doesn't. The former case is represented by `undefined` (due to the
* semantics of {@link Map.get}), while the latter is represented by the
* sentinel value.
*/
private languageDefinitions: Map<
string,
LanguageDefinition | typeof LANGUAGE_UNDEFINED
> = new Map();

constructor(private treeSitter: TreeSitter) {}

/**
* Get a language definition for the given language id, if the language
* has a new-style query definition, or return undefined if the language doesn't
*
* @param languageId The language id for which to get a language definition
* @returns A language definition for the given language id, or undefined if
* the given language id doesn't have a new-style query definition
*/
get(languageId: string): LanguageDefinition | undefined {
let definition = this.languageDefinitions.get(languageId);

if (definition == null) {
definition =
LanguageDefinition.create(this.treeSitter, languageId as LanguageId) ??
LANGUAGE_UNDEFINED;

this.languageDefinitions.set(languageId, definition);
}

export interface LanguageDefinition {
maybeGetLanguageScopeHandler: (
scopeType: ScopeType,
) => ScopeHandler | undefined;
return definition === LANGUAGE_UNDEFINED ? undefined : definition;
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/languages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { SupportedLanguageId, supportedLanguageIds } from "./constants";
export function isLanguageSupported(
languageId: string,
): languageId is SupportedLanguageId {
return languageId in supportedLanguageIds;
return supportedLanguageIds.includes(languageId as SupportedLanguageId);
}
1 change: 0 additions & 1 deletion packages/cursorless-engine/src/languages/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ const nodeMatchers: Partial<
"argument_list",
),
collectionKey: trailingMatcher(["pair[key]"], [":"]),
className: "class[name]",
name: [
"assignment[left]",
"operator_assignment[left]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
default:
return this.languageDefinitions
.get(languageId)
?.maybeGetLanguageScopeHandler(scopeType);
?.getScopeHandler(scopeType);
}
}
}
Loading