This repository has been archived on 2020-04-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Vim-Vixen/src/background/controllers/CommandController.ts
2019-05-02 22:07:53 +09:00

103 lines
3 KiB
TypeScript

import CompletionsUseCase from '../usecases/CompletionsUseCase';
import CommandUseCase from '../usecases/CommandUseCase';
import CompletionGroup from '../domains/CompletionGroup';
const trimStart = (str: string): string => {
// NOTE String.trimStart is available on Firefox 61
return str.replace(/^\s+/, '');
};
export default class CommandController {
private completionsUseCase: CompletionsUseCase;
private commandIndicator: CommandUseCase;
constructor() {
this.completionsUseCase = new CompletionsUseCase();
this.commandIndicator = new CommandUseCase();
}
getCompletions(line: string): Promise<CompletionGroup[]> {
let trimmed = trimStart(line);
let words = trimmed.split(/ +/);
let name = words[0];
if (words.length === 1) {
return this.completionsUseCase.queryConsoleCommand(name);
}
let keywords = trimStart(trimmed.slice(name.length));
switch (words[0]) {
case 'o':
case 'open':
case 't':
case 'tabopen':
case 'w':
case 'winopen':
return this.completionsUseCase.queryOpen(name, keywords);
case 'b':
case 'buffer':
return this.completionsUseCase.queryBuffer(name, keywords);
case 'bd':
case 'bdel':
case 'bdelete':
case 'bdeletes':
return this.completionsUseCase.queryBdelete(name, keywords);
case 'bd!':
case 'bdel!':
case 'bdelete!':
case 'bdeletes!':
return this.completionsUseCase.queryBdeleteForce(name, keywords);
case 'set':
return this.completionsUseCase.querySet(name, keywords);
}
return Promise.resolve([]);
}
// eslint-disable-next-line complexity
exec(line: string): Promise<any> {
let trimmed = trimStart(line);
let words = trimmed.split(/ +/);
let name = words[0];
if (words[0].length === 0) {
return Promise.resolve();
}
let keywords = trimStart(trimmed.slice(name.length));
switch (words[0]) {
case 'o':
case 'open':
return this.commandIndicator.open(keywords);
case 't':
case 'tabopen':
return this.commandIndicator.tabopen(keywords);
case 'w':
case 'winopen':
return this.commandIndicator.winopen(keywords);
case 'b':
case 'buffer':
return this.commandIndicator.buffer(keywords);
case 'bd':
case 'bdel':
case 'bdelete':
return this.commandIndicator.bdelete(false, keywords);
case 'bd!':
case 'bdel!':
case 'bdelete!':
return this.commandIndicator.bdelete(true, keywords);
case 'bdeletes':
return this.commandIndicator.bdeletes(false, keywords);
case 'bdeletes!':
return this.commandIndicator.bdeletes(true, keywords);
case 'addbookmark':
return this.commandIndicator.addbookmark(keywords);
case 'q':
case 'quit':
return this.commandIndicator.quit();
case 'qa':
case 'quitall':
return this.commandIndicator.quitAll();
case 'set':
return this.commandIndicator.set(keywords);
}
throw new Error(words[0] + ' command is not defined');
}
}