diff --git a/src/Parser.ts b/src/Parser.ts index d3816f939..0743dacd7 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -26,8 +26,6 @@ export enum Progress { export type Parser = (context: Context) => Promise>; export interface Result { - parser: Parser; - context: Context; parse: string; progress: Progress; suggestions: Suggestion[]; @@ -83,13 +81,11 @@ export const bind = (left: Parser, rightGenerator: (result: Result) => Promise

Suggestio const results = await parser(context); return results.map(result => ({ - parser: decorate(result.parser, decorator), - context: result.context, parse: result.parse, progress: result.progress, suggestions: result.suggestions.map(decorator), @@ -184,7 +178,6 @@ export const runtime = (producer: (context: Context) => Promise) => asyn }; export const optionalContinuation = (parser: Parser) => optional(sequence(spacesWithoutSuggestion, parser)); -export const append = (suffix: string, parser: Parser) => decorate(sequence(parser, withoutSuggestions(string(suffix))), suggestion => suggestion.withValue(suggestion.value + suffix)); export const token = (parser: Parser) => decorate(sequence(parser, spacesWithoutSuggestion), suggestion => suggestion.withValue(suggestion.value + " ")); export const executable = (name: string) => decorate(token(string(name)), style(styles.executable)); export const commandSwitch = (value: string) => decorate(string(`--${value}`), style(styles.option)); diff --git a/src/plugins/autocompletion_providers/Common.ts b/src/plugins/autocompletion_providers/Common.ts index ec0cab353..7c9fcdacb 100644 --- a/src/plugins/autocompletion_providers/Common.ts +++ b/src/plugins/autocompletion_providers/Common.ts @@ -17,29 +17,32 @@ const pathParser = (name: string) => { return decorate(parser, suggestion => suggestion.withDisplayValue(name).withValue(suggestion.value.replace(/\s/g, "\\ "))); }; const fileParser = (info: FileInfo) => decorate(pathParser(info.name), style(styles.file(info))); + const directoryParser = (name: string) => decorate(pathParser(name), style(styles.directory)); const directoryAlias = (workingDirectory: string, filter: FileFilter) => (name: string) => sequence( withoutSuggestions(directoryParser(name)), pathIn(resolveDirectory(workingDirectory, name), filter) ); -export const pathIn = (directory: string, filter: (info: FileInfo) => boolean): Parser => runtime(async() => { - const stats = await statsIn(directory); +export function pathIn(directory: string, filter: (info: FileInfo) => boolean): Parser { + return runtime(async() => { + const stats = await statsIn(directory); - return choice([ - ...stats.filter(filter).map(info => { - if (info.stat.isDirectory()) { - return sequence( - directoryParser(`${info.name}/`), - pathIn(resolveDirectory(directory, info.name), filter) - ); - } else { - return fileParser(info); - } - }), - ...["./", "../"].map(directoryAlias(directory, filter)) - ]); -}); + return choice([ + ...stats.filter(filter).map(info => { + if (info.stat.isDirectory()) { + return sequence( + directoryParser(`${info.name}/`), + pathIn(resolveDirectory(directory, info.name), filter) + ); + } else { + return fileParser(info); + } + }), + ...["./", "../"].map(directoryAlias(directory, filter)), + ]); + }); +} export const pathInCurrentDirectory = (filter: FileFilter) => runtime(async(context) => choice([ ...["/", "~/"].map(directoryAlias(context.directory, filter)), diff --git a/test/parser_spec.ts b/test/parser_spec.ts index 4fb8890b9..428d5723d 100644 --- a/test/parser_spec.ts +++ b/test/parser_spec.ts @@ -25,28 +25,28 @@ describe("parser", () => { const results = await parse(parser, ""); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(git); + expect(suggestionDisplayValues(results)).to.eql(["git "]); }); it("derives the first parser for a part of first parsers' input", async() => { const results = await parse(parser, "gi"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(git); + expect(suggestionDisplayValues(results)).to.eql(["git "]); }); it("derives the first parser if the input exactly matches the first parser", async() => { const results = await parse(parser, "git "); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(git); + expect(suggestionDisplayValues(results)).to.eql(["git "]); }); it("derives the second parser if the input exceeds the first parser", async() => { const results = await parse(parser, "git c"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(commit); + expect(suggestionDisplayValues(results)).to.eql(["commit"]); }); }); @@ -61,7 +61,6 @@ describe("parser", () => { expect(results.length).to.equal(1); expect(results[0]).to.deep.include({ - parser: space, parse: "git ", }); }); @@ -72,7 +71,6 @@ describe("parser", () => { expect(results.length).to.equal(1); expect(results[0]).to.deep.include({ - parser: space, parse: "git ", }); }); @@ -99,7 +97,7 @@ describe("parser", () => { const results = await parse(parser, "git c"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(parser); + expect(suggestionDisplayValues(results)).to.eql(["grep"]); }); }); @@ -111,7 +109,7 @@ describe("parser", () => { const results = await parse(choice([left, right]), "f"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(left); + expect(suggestionDisplayValues(results)).to.eql(["foo"]); }); it("derives the right parser if the left one doesn't match", async() => { @@ -121,7 +119,7 @@ describe("parser", () => { const results = await parse(choice([left, right]), "b"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(right); + expect(suggestionDisplayValues(results)).to.eql(["bar"]); }); it("derives both parsers if they match", async() => { @@ -131,7 +129,7 @@ describe("parser", () => { const results = await parse(choice([soon, sooner]), "soo"); expect(results.length).to.equal(2); - expect(results.map(result => result.parser)).to.eql([soon, sooner]); + expect(suggestionDisplayValues(results)).to.eql(["soon", "sooner"]); }); it("doesn't commit to a branch too early", async() => { @@ -139,7 +137,7 @@ describe("parser", () => { const results = await parse(sequence(sequence(string("git"), choice([string(" "), string(" ")])), commit), "git commit"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(commit); + expect(suggestionDisplayValues(results)).to.eql(["commit"]); }); }); @@ -151,14 +149,14 @@ describe("parser", () => { const results = await parse(parser, "git c"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(commit); + expect(suggestionDisplayValues(results)).to.eql(["commit"]); }); it("matches two occurrences", async() => { const results = await parse(parser, "git c"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(commit); + expect(suggestionDisplayValues(results)).to.eql(["commit"]); }); }); @@ -168,7 +166,7 @@ describe("parser", () => { const results = await parse(sequence(optional(string("sudo ")), git), "g"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(git); + expect(suggestionDisplayValues(results)).to.eql(["git"]); }); it("matches with an occurrence", async() => { @@ -176,7 +174,7 @@ describe("parser", () => { const results = await parse(sequence(optional(string("sudo ")), git), "sudo g"); expect(results.length).to.equal(1); - expect(results[0].parser).to.equal(git); + expect(suggestionDisplayValues(results)).to.eql(["git"]); }); }); });