Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Cleanup Result.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed Jun 1, 2016
1 parent b831e96 commit bdde0fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
9 changes: 1 addition & 8 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export enum Progress {
export type Parser = (context: Context) => Promise<Array<Result>>;

export interface Result {
parser: Parser;
context: Context;
parse: string;
progress: Progress;
suggestions: Suggestion[];
Expand Down Expand Up @@ -83,13 +81,11 @@ export const bind = (left: Parser, rightGenerator: (result: Result) => Promise<P

if (leftResult.progress === Progress.Finished && (rightInput.length || leftResult.suggestions.length === 0)) {
const right = await rightGenerator(leftResult);
const rightResults = await right(Object.assign({}, leftResult.context, { input: rightInput }));
const rightResults = await right(Object.assign({}, context, { input: rightInput }));

for (const rightResult of rightResults) {
if (rightResult.progress !== Progress.Failed) {
results.push({
parser: rightResult.parser,
context: rightResult.context,
parse: leftResult.parse + rightResult.parse,
progress: rightResult.progress,
suggestions: rightResult.suggestions,
Expand Down Expand Up @@ -143,8 +139,6 @@ export const decorate = (parser: Parser, decorator: (s: Suggestion) => 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),
Expand Down Expand Up @@ -184,7 +178,6 @@ export const runtime = (producer: (context: Context) => Promise<Parser>) => 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));
Expand Down
35 changes: 19 additions & 16 deletions src/plugins/autocompletion_providers/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
28 changes: 13 additions & 15 deletions test/parser_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
});
});

Expand All @@ -61,7 +61,6 @@ describe("parser", () => {

expect(results.length).to.equal(1);
expect(results[0]).to.deep.include({
parser: space,
parse: "git ",
});
});
Expand All @@ -72,7 +71,6 @@ describe("parser", () => {

expect(results.length).to.equal(1);
expect(results[0]).to.deep.include({
parser: space,
parse: "git ",
});
});
Expand All @@ -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"]);
});
});

Expand All @@ -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() => {
Expand All @@ -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() => {
Expand All @@ -131,15 +129,15 @@ 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() => {
const commit = string("commit");
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"]);
});
});

Expand All @@ -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"]);
});
});

Expand All @@ -168,15 +166,15 @@ 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() => {
const git = string("git");
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"]);
});
});
});

0 comments on commit bdde0fb

Please sign in to comment.