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

Commit ae7e6b4

Browse files
committed
Remove the Suggestion class.
1 parent 457a466 commit ae7e6b4

File tree

18 files changed

+137
-164
lines changed

18 files changed

+137
-164
lines changed

src/Autocompletion.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {leafNodeAt, ASTNode} from "./shell/Parser";
22
import * as _ from "lodash";
33
import {History} from "./shell/History";
4-
import {Suggestion, styles, replaceAllPromptSerializer} from "./plugins/autocompletion_utils/Common";
4+
import {
5+
styles, replaceAllPromptSerializer, Suggestion,
6+
SuggestionWithDefaults, addDefaultAttributeValues,
7+
} from "./plugins/autocompletion_utils/Common";
58
import {Environment} from "./shell/Environment";
69
import {OrderedSet} from "./utils/OrderedSet";
710
import {Aliases} from "./shell/Aliases";
@@ -24,9 +27,9 @@ export const getSuggestions = async({
2427
environment,
2528
historicalPresentDirectoriesStack,
2629
aliases,
27-
}: GetSuggestionsOptions): Promise<Suggestion[]> => {
30+
}: GetSuggestionsOptions): Promise<SuggestionWithDefaults[]> => {
2831
const prefixMatchesInHistory = History.all.filter(line => line.startsWith(currentText));
29-
const suggestionsFromHistory = prefixMatchesInHistory.map(match => new Suggestion({
32+
const suggestionsFromHistory = prefixMatchesInHistory.map(match => ({
3033
value: match,
3134
promptSerializer: replaceAllPromptSerializer,
3235
isFiltered: true,
@@ -44,7 +47,7 @@ export const getSuggestions = async({
4447
});
4548

4649
const uniqueSuggestions = _.uniqBy([...firstThreeFromHistory, ...suggestions, ...remainderFromHistory], suggestion => suggestion.value);
47-
const applicableSuggestions = uniqueSuggestions.filter(suggestion => suggestion.isFiltered || suggestion.value.toLowerCase().startsWith(node.value.toLowerCase()));
50+
const applicableSuggestions: Suggestion[] = uniqueSuggestions.filter(suggestion => suggestion.isFiltered || suggestion.value.toLowerCase().startsWith(node.value.toLowerCase()));
4851

4952
if (applicableSuggestions.length === 1) {
5053
const suggestion = applicableSuggestions[0];
@@ -53,10 +56,10 @@ export const getSuggestions = async({
5356
* The suggestion would simply duplicate the prompt value without providing no
5457
* additional information. Skipping it for clarity.
5558
*/
56-
if (node.value === suggestion.value && suggestion.description.length === 0 && suggestion.synopsis.length === 0) {
59+
if (node.value === suggestion.value && (suggestion.description && suggestion.description.length === 0) && (suggestion.synopsis && suggestion.synopsis.length === 0)) {
5760
return [];
5861
}
5962
}
6063

61-
return applicableSuggestions.slice(0, suggestionsLimit);
64+
return applicableSuggestions.slice(0, suggestionsLimit).map(addDefaultAttributeValues);
6265
};

src/Interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Stats} from "fs";
33
import {ReactElement} from "react";
44
import {Job} from "./shell/Job";
55
import {Session} from "./shell/Session";
6-
import {PartialSuggestion} from "./plugins/autocompletion_utils/Common";
6+
import {Suggestion} from "./plugins/autocompletion_utils/Common";
77
import {ScreenBuffer} from "./ScreenBuffer";
88
import {Environment} from "./shell/Environment";
99
import {OrderedSet} from "./utils/OrderedSet";
@@ -34,7 +34,7 @@ export interface AutocompletionContext extends PreliminaryAutocompletionContext
3434
readonly argument: Argument;
3535
}
3636

37-
export type AutocompletionProvider = (context: AutocompletionContext) => Promise<PartialSuggestion[]>;
37+
export type AutocompletionProvider = (context: AutocompletionContext) => Promise<Suggestion[]>;
3838

3939
export interface FileInfo {
4040
name: string;

src/plugins/autocompletion_providers/Brew.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
styles, Suggestion, longFlag, contextIndependent,
3-
emptyProvider, shortFlag, provide,
2+
styles, longFlag, contextIndependent,
3+
emptyProvider, shortFlag, provide, Suggestion,
44
} from "../autocompletion_utils/Common";
55
import combine from "../autocompletion_utils/Combine";
66
import {PluginManager} from "../../PluginManager";
@@ -44,7 +44,7 @@ const formulaSuggestions = async(formulae: FormulaAttributes[],
4444
.filter(formula => !query ||
4545
formula.name.startsWith(query) ||
4646
formula.path.startsWith(query))
47-
.map(formula => new Suggestion({
47+
.map(formula => ({
4848
value: formula.name,
4949
displayValue: formula.name,
5050
synopsis: formula.path,
@@ -310,7 +310,7 @@ const brewCommands: BrewCommandData[] = [
310310
const fromData = (commandsData: BrewCommandData[]) =>
311311
contextIndependent(async() => {
312312
const suggestions = commandsData
313-
.map(command => new Suggestion({
313+
.map(command => ({
314314
value: command.name,
315315
description: command.description || "",
316316
style: styles.command,

src/plugins/autocompletion_providers/Cd.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expandHistoricalDirectory} from "../../shell/Command";
2-
import {styles, Suggestion, directoriesSuggestionsProvider} from "../autocompletion_utils/Common";
2+
import {styles, directoriesSuggestionsProvider, Suggestion} from "../autocompletion_utils/Common";
33
import * as _ from "lodash";
44
import {PluginManager} from "../../PluginManager";
55

@@ -12,7 +12,7 @@ PluginManager.registerAutocompletionProvider("cd", async(context) => {
1212
if (context.argument.value.startsWith("-")) {
1313
const historicalDirectoryAliases = ["-", "-2", "-3", "-4", "-5", "-6", "-7", "-8", "-9"]
1414
.slice(0, context.historicalPresentDirectoriesStack.size)
15-
.map(alias => new Suggestion({
15+
.map(alias => ({
1616
value: alias,
1717
description: expandHistoricalDirectory(alias, context.historicalPresentDirectoriesStack),
1818
style: styles.directory,
@@ -26,7 +26,7 @@ PluginManager.registerAutocompletionProvider("cd", async(context) => {
2626
if (context.argument.value.length > 0) {
2727
const cdpathDirectories = _.flatten(await Promise.all(context.environment.cdpath
2828
.filter(directory => directory !== context.environment.pwd)
29-
.map(async(directory) => (await directoriesSuggestionsProvider(context, directory)).map(suggestion => suggestion.withDescription(`In ${directory}`)))));
29+
.map(async(directory) => (await directoriesSuggestionsProvider(context, directory)).map(suggestion => ({...suggestion, description: `In ${directory}`})))));
3030

3131
suggestions.push(...cdpathDirectories);
3232
}

src/plugins/autocompletion_providers/Git.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Git from "../../utils/Git";
22
import {
3-
styles, Suggestion, longAndShortFlag, longFlag, mapSuggestions, unique,
3+
styles, longAndShortFlag, longFlag, mapSuggestions, unique,
44
emptyProvider, SubcommandConfig, commandWithSubcommands, provide,
55
} from "../autocompletion_utils/Common";
66
import * as Common from "../autocompletion_utils/Common";
@@ -10,14 +10,14 @@ import {linedOutputOf, executeCommand} from "../../PTY";
1010
import {find, sortBy, once} from "lodash";
1111

1212
const addOptions = combine([
13-
mapSuggestions(longAndShortFlag("patch"), suggestion => suggestion.withDescription(
13+
mapSuggestions(longAndShortFlag("patch"), suggestion => ({...suggestion, description:
1414
`Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the
1515
difference before adding modified contents to the index.
1616
This effectively runs add --interactive, but bypasses the initial command menu and directly jumps to the patch subcommand. See "Interactive
17-
mode" for details.`)),
18-
mapSuggestions(longFlag("interactive"), suggestion => suggestion.withDescription(`
17+
mode" for details.`})),
18+
mapSuggestions(longFlag("interactive"), suggestion => ({...suggestion, description: `
1919
Add modified contents in the working tree interactively to the index. Optional path arguments may be supplied to limit operation to a subset
20-
of the working tree. See "Interactive mode" for details.`)),
20+
of the working tree. See "Interactive mode" for details.`})),
2121
]);
2222

2323
function doesLookLikeBranchAlias(word: string) {
@@ -187,7 +187,7 @@ const commitOptionsData: OptionData[] = [
187187

188188
const commitOptions = combine(commitOptionsData.map(({ longFlag, shortFlag, noShortFlag, description }) => {
189189
const provider = noShortFlag ? Common.longFlag(longFlag) : longAndShortFlag(longFlag, shortFlag);
190-
return mapSuggestions(provider, suggestion => suggestion.withDescription(description));
190+
return mapSuggestions(provider, suggestion => ({...suggestion, description}));
191191
}));
192192

193193
const pushOptions = combine([
@@ -248,7 +248,7 @@ const commonMergeOptions = combine([
248248
const remotes = provide(async context => {
249249
if (Git.isGitDirectory(context.environment.pwd)) {
250250
const names = await Git.remotes(context.environment.pwd);
251-
return names.map(name => new Suggestion({value: name, style: styles.branch}));
251+
return names.map(name => ({value: name, style: styles.branch}));
252252
}
253253

254254
return [];
@@ -257,7 +257,7 @@ const remotes = provide(async context => {
257257
const configVariables = unique(provide(async context => {
258258
const variables = await Git.configVariables(context.environment.pwd);
259259

260-
return variables.map(variable => new Suggestion({value: variable.name, description: variable.value, style: styles.option}));
260+
return variables.map(variable => ({value: variable.name, description: variable.value, style: styles.option}));
261261
}));
262262

263263
const branchesExceptCurrent = provide(async context => {
@@ -268,7 +268,7 @@ const branchesExceptCurrent = provide(async context => {
268268
tags: false,
269269
}));
270270
const nonCurrentBranches = allBranches.filter(branch => !branch.isCurrent());
271-
return nonCurrentBranches.map(branch => new Suggestion({value: branch.toString(), style: styles.branch}));
271+
return nonCurrentBranches.map(branch => ({value: branch.toString(), style: styles.branch}));
272272
} else {
273273
return [];
274274
}
@@ -278,7 +278,7 @@ const branchAlias = provide(async context => {
278278
if (doesLookLikeBranchAlias(context.argument.value)) {
279279
let nameOfAlias = (await linedOutputOf("git", ["name-rev", "--name-only", canonizeBranchAlias(context.argument.value)], context.environment.pwd))[0];
280280
if (nameOfAlias && !nameOfAlias.startsWith("Could not get")) {
281-
return [new Suggestion({value: context.argument.value, synopsis: nameOfAlias, style: styles.branch})];
281+
return [{value: context.argument.value, synopsis: nameOfAlias, style: styles.branch}];
282282
}
283283
}
284284

@@ -288,7 +288,7 @@ const branchAlias = provide(async context => {
288288
const notStagedFiles = unique(provide(async context => {
289289
if (Git.isGitDirectory(context.environment.pwd)) {
290290
const fileStatuses = await Git.status(context.environment.pwd);
291-
return fileStatuses.map(fileStatus => new Suggestion({value: fileStatus.value, style: styles.gitFileStatus(fileStatus.code)}));
291+
return fileStatuses.map(fileStatus => ({value: fileStatus.value, style: styles.gitFileStatus(fileStatus.code)}));
292292
} else {
293293
return [];
294294
}

src/plugins/autocompletion_providers/Grep.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {PluginManager} from "../../PluginManager";
22
import {
33
longFlag, longAndShortFlag, mapSuggestions, anyFilesSuggestionsProvider,
4-
Suggestion, styles, anyFilesSuggestions, directoriesSuggestions, provide,
4+
styles, anyFilesSuggestions, directoriesSuggestions, provide,
55
} from "../autocompletion_utils/Common";
66
import combine from "../autocompletion_utils/Combine";
77
import {io, mapObject} from "../../utils/Common";
@@ -207,24 +207,29 @@ const baseOptions = combine(mapObject(
207207
},
208208
(option, info) => {
209209
if (info.short) {
210-
return mapSuggestions(longAndShortFlag(option, info.short),
211-
suggestion => suggestion.withDescription(info.description));
210+
return mapSuggestions(
211+
longAndShortFlag(option, info.short),
212+
suggestion => ({...suggestion, description: info.description}),
213+
);
212214
} else {
213-
return mapSuggestions(longFlag(option),
214-
suggestion => suggestion.withDescription(info.description));
215+
return mapSuggestions(
216+
longFlag(option),
217+
suggestion => ({...suggestion, description: info.description}),
218+
);
215219
}
216220
},
217221
));
218222

219223
const extendedRegexOption = combine([
220-
mapSuggestions(longAndShortFlag("extended-regexp", "E"), suggestion => suggestion.withDescription(`
221-
Interpret <pattern> (defined using --regexp=<pattern> as an extended regular expression`)),
224+
mapSuggestions(
225+
longAndShortFlag("extended-regexp", "E"),
226+
suggestion => ({...suggestion, description: `Interpret <pattern> (defined using --regexp=<pattern> as an extended regular expression`})),
222227
]);
223228

224229
const fixedStringsOption = combine([
225-
mapSuggestions(longAndShortFlag("fixed-strings", "F"), suggestion => suggestion.withDescription(`
226-
Interpret <pattern> (defined using --regexp=<pattern>) as a list of fixed strings, separated
227-
by new-lines lines, any of which is to be matched`)),
230+
mapSuggestions(
231+
longAndShortFlag("fixed-strings", "F"),
232+
suggestion => ({...suggestion, description: `Interpret <pattern> (defined using --regexp=<pattern>) as a list of fixed strings, separated by new-lines lines, any of which is to be matched`})),
228233
]);
229234

230235
const binaryFilesValues = [
@@ -307,9 +312,12 @@ const fixedValueSuggestions = provide(async context => {
307312
} else {
308313
return [];
309314
}
310-
return optionValues.map(item =>
311-
new Suggestion({value: "--" + item.flag + "=" + item.displayValue, displayValue: item.displayValue,
312-
description: item.description, style: styles.optionValue}));
315+
return optionValues.map(item => ({
316+
value: "--" + item.flag + "=" + item.displayValue,
317+
displayValue: item.displayValue,
318+
description: item.description,
319+
style: styles.optionValue,
320+
}));
313321
});
314322

315323
const fileValueSuggestions = provide(async context => {
@@ -321,7 +329,7 @@ const fileValueSuggestions = provide(async context => {
321329
const optionValue = token.slice(tokenValue.length);
322330
const fileSuggestions = await anyFilesSuggestions(optionValue, workingDirectory);
323331
return fileSuggestions.map(item =>
324-
new Suggestion({value: tokenValue + item.value, displayValue: item.displayValue,
332+
({value: tokenValue + item.value, displayValue: item.displayValue,
325333
style: io.directoryExists(workingDirectory + item.value) ? styles.directory : styles.optionValue}));
326334
} else {
327335
return [];
@@ -336,9 +344,11 @@ const excludeFromSuggestions = provide(async context => {
336344
const workingDirectory = context.environment.pwd;
337345
const optionValue = token.slice(tokenValue.length);
338346
const fileSuggestions = await anyFilesSuggestions(optionValue, workingDirectory);
339-
return fileSuggestions.map(item =>
340-
new Suggestion({value: tokenValue + item.value, displayValue: item.displayValue,
341-
style: io.directoryExists(workingDirectory + item.value) ? styles.directory : styles.optionValue}));
347+
return fileSuggestions.map(item => ({
348+
value: tokenValue + item.value,
349+
displayValue: item.displayValue,
350+
style: io.directoryExists(workingDirectory + item.value) ? styles.directory : styles.optionValue,
351+
}));
342352
} else {
343353
return [];
344354
}
@@ -353,7 +363,7 @@ const excludeDirSuggestions = provide(async context => {
353363
const optionValue = token.slice(tokenValue.length);
354364
const directorySuggestions = await directoriesSuggestions(optionValue, workingDirectory);
355365
return directorySuggestions.map(item =>
356-
new Suggestion({value: tokenValue + item.value, displayValue: item.displayValue,
366+
({value: tokenValue + item.value, displayValue: item.displayValue,
357367
style: styles.directory}));
358368
} else {
359369
return [];

src/plugins/autocompletion_providers/NPM.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Path from "path";
2-
import {Suggestion, styles, commandWithSubcommands} from "../autocompletion_utils/Common";
2+
import {styles, commandWithSubcommands} from "../autocompletion_utils/Common";
33
import {io, mapObject} from "../../utils/Common";
44
import {PluginManager} from "../../PluginManager";
55
import {AutocompletionContext} from "../../Interfaces";
@@ -152,7 +152,7 @@ const npmCommandConfig = [
152152
const packageFilePath = Path.join(context.environment.pwd, "package.json");
153153
if (await io.fileExists(packageFilePath)) {
154154
const parsed = JSON.parse(await io.readFile(packageFilePath)).scripts || {};
155-
return mapObject(parsed, (key: string, value: string) => new Suggestion({
155+
return mapObject(parsed, (key: string, value: string) => ({
156156
value: key,
157157
description: value,
158158
style: styles.command,

0 commit comments

Comments
 (0)