-
-
Notifications
You must be signed in to change notification settings - Fork 88
Support specializing snippet definitions by containing scope #1487
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
AndreasArvidsson
merged 4 commits into
main
from
pokey/support-specializing-snippet-definitions-by-containing-scope
May 24, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { showError, Snippet, SnippetMap, walkFiles } from "@cursorless/common"; | ||
import { readFile, stat } from "fs/promises"; | ||
import { cloneDeep, max, merge } from "lodash"; | ||
import { max } from "lodash"; | ||
import { join } from "path"; | ||
import { ide } from "../singletons/ide.singleton"; | ||
import { mergeStrict } from "../util/object"; | ||
import { mergeSnippets } from "./mergeSnippets"; | ||
|
||
const CURSORLESS_SNIPPETS_SUFFIX = ".cursorless-snippets"; | ||
const SNIPPET_DIR_REFRESH_INTERVAL_MS = 1000; | ||
|
@@ -21,7 +22,7 @@ interface DirectoryErrorMessage { | |
export class Snippets { | ||
private coreSnippets!: SnippetMap; | ||
private thirdPartySnippets: Record<string, SnippetMap> = {}; | ||
private userSnippets!: SnippetMap; | ||
private userSnippets!: SnippetMap[]; | ||
|
||
private mergedSnippets!: SnippetMap; | ||
|
||
|
@@ -133,7 +134,7 @@ export class Snippets { | |
}; | ||
} | ||
|
||
this.userSnippets = {}; | ||
this.userSnippets = []; | ||
this.mergeSnippets(); | ||
|
||
return; | ||
|
@@ -154,34 +155,32 @@ export class Snippets { | |
|
||
this.maxSnippetMtimeMs = maxSnippetMtime; | ||
|
||
this.userSnippets = mergeStrict( | ||
...(await Promise.all( | ||
snippetFiles.map(async (path) => { | ||
try { | ||
const content = await readFile(path, "utf8"); | ||
|
||
if (content.length === 0) { | ||
// Gracefully handle an empty file | ||
return {}; | ||
} | ||
|
||
return JSON.parse(content); | ||
} catch (err) { | ||
showError( | ||
ide().messages, | ||
"snippetsFileError", | ||
`Error with cursorless snippets file "${path}": ${ | ||
(err as Error).message | ||
}`, | ||
); | ||
|
||
// We don't want snippets from all files to stop working if there is | ||
// a parse error in one file, so we just effectively ignore this file | ||
// once we've shown an error message | ||
this.userSnippets = await Promise.all( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't merge here; we handle things more carefully now, when we do the final merge below |
||
snippetFiles.map(async (path) => { | ||
try { | ||
const content = await readFile(path, "utf8"); | ||
|
||
if (content.length === 0) { | ||
// Gracefully handle an empty file | ||
return {}; | ||
} | ||
}), | ||
)), | ||
|
||
return JSON.parse(content); | ||
} catch (err) { | ||
showError( | ||
ide().messages, | ||
"snippetsFileError", | ||
`Error with cursorless snippets file "${path}": ${ | ||
(err as Error).message | ||
}`, | ||
); | ||
|
||
// We don't want snippets from all files to stop working if there is | ||
// a parse error in one file, so we just effectively ignore this file | ||
// once we've shown an error message | ||
return {}; | ||
} | ||
}), | ||
); | ||
|
||
this.mergeSnippets(); | ||
|
@@ -206,34 +205,11 @@ export class Snippets { | |
* party > core. | ||
*/ | ||
private mergeSnippets() { | ||
this.mergedSnippets = {}; | ||
|
||
// We make a list of all entries from all sources, in order of increasing | ||
// precedence: user > third party > core. | ||
const entries = [ | ||
...Object.entries(cloneDeep(this.coreSnippets)), | ||
...Object.values(this.thirdPartySnippets).flatMap((snippets) => | ||
Object.entries(cloneDeep(snippets)), | ||
), | ||
...Object.entries(cloneDeep(this.userSnippets)), | ||
]; | ||
|
||
entries.forEach(([key, value]) => { | ||
if (Object.prototype.hasOwnProperty.call(this.mergedSnippets, key)) { | ||
const { definitions, ...rest } = value; | ||
const mergedSnippet = this.mergedSnippets[key]; | ||
|
||
// NB: We make sure that the new definitions appear before the previous | ||
// ones so that they take precedence | ||
mergedSnippet.definitions = definitions.concat( | ||
...mergedSnippet.definitions, | ||
); | ||
|
||
merge(mergedSnippet, rest); | ||
} else { | ||
this.mergedSnippets[key] = value; | ||
} | ||
}); | ||
this.mergedSnippets = mergeSnippets( | ||
this.coreSnippets, | ||
this.thirdPartySnippets, | ||
this.userSnippets, | ||
); | ||
} | ||
|
||
/** | ||
|
95 changes: 95 additions & 0 deletions
95
packages/cursorless-engine/src/core/compareSnippetDefinitions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
SimpleScopeTypeType, | ||
SnippetDefinition, | ||
SnippetScope, | ||
} from "@cursorless/common"; | ||
import { SnippetOrigin } from "./mergeSnippets"; | ||
|
||
/** | ||
* Compares two snippet definitions by how specific their scope, breaking | ||
* ties by origin. | ||
* @param a One of the snippet definitions to compare | ||
* @param b The other snippet definition to compare | ||
* @returns A negative number if a should come before b, a positive number if b | ||
*/ | ||
export function compareSnippetDefinitions( | ||
a: SnippetDefinitionWithOrigin, | ||
b: SnippetDefinitionWithOrigin, | ||
): number { | ||
const scopeComparision = compareSnippetScopes( | ||
a.definition.scope, | ||
b.definition.scope, | ||
); | ||
|
||
// Prefer the more specific snippet definitino, no matter the origin | ||
if (scopeComparision !== 0) { | ||
return scopeComparision; | ||
} | ||
|
||
// If the scopes are the same, prefer the snippet from the higher priority | ||
// origin | ||
return a.origin - b.origin; | ||
} | ||
|
||
function compareSnippetScopes( | ||
a: SnippetScope | undefined, | ||
b: SnippetScope | undefined, | ||
): number { | ||
if (a == null && b == null) { | ||
return 0; | ||
} | ||
|
||
// Prefer the snippet that has a scope at all | ||
if (a == null) { | ||
return -1; | ||
} | ||
|
||
if (b == null) { | ||
return 1; | ||
} | ||
|
||
// Prefer the snippet that is language-specific, regardless of scope type | ||
if (a.langIds == null && b.langIds != null) { | ||
return -1; | ||
} | ||
|
||
if (b.langIds == null && a.langIds != null) { | ||
return 1; | ||
} | ||
|
||
// If both snippets are language-specific, prefer the snippet that specifies | ||
// scope types. Note that this holds even if one snippet specifies more | ||
// languages than the other. The motivating use case is if you have a snippet | ||
// for functions in js and ts, and a snippet for methods in js and ts. If you | ||
// override the function snippet for ts, you still want the method snippet to | ||
// be used for ts methods. | ||
const scopeTypesComparision = compareScopeTypes(a.scopeTypes, b.scopeTypes); | ||
|
||
if (scopeTypesComparision !== 0) { | ||
return scopeTypesComparision; | ||
} | ||
|
||
// If snippets both have scope types or both don't have scope types, prefer | ||
// the snippet that specifies fewer languages | ||
return a.langIds == null ? 0 : b.langIds!.length - a.langIds.length; | ||
} | ||
|
||
function compareScopeTypes( | ||
a: SimpleScopeTypeType[] | undefined, | ||
b: SimpleScopeTypeType[] | undefined, | ||
): number { | ||
if (a == null && b != null) { | ||
return -1; | ||
} | ||
|
||
if (b == null && a != null) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
interface SnippetDefinitionWithOrigin { | ||
origin: SnippetOrigin; | ||
definition: SnippetDefinition; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.