Skip to content

Commit

Permalink
Refactor code with enumerator
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Jul 27, 2016
1 parent d759f3d commit 08783b4
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 262 deletions.
4 changes: 2 additions & 2 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ class MCommandMoveHalfPageDown extends BaseMovement {

public async execAction(position: Position, vimState: VimState): Promise<Position> {
return new Position(
Math.min(TextEditor.getLineCount() - 1, position.line + <number> Configuration.getInstance().get("scroll")),
Math.min(TextEditor.getLineCount() - 1, position.line + Configuration.getInstance().scroll),
position.character
);
}
Expand All @@ -1267,7 +1267,7 @@ class MCommandMoveHalfPageUp extends BaseMovement {
keys = ["ctrl+u"];

public async execAction(position: Position, vimState: VimState): Promise<Position> {
return new Position(Math.max(0, position.line - <number> Configuration.getInstance().get("scroll")), position.character);
return new Position(Math.max(0, position.line - Configuration.getInstance().scroll), position.character);
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/cmd_line/commands/setoptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export enum SetOptionOperator {
* Toggle option: Reset, switch it off.
*/
Reset,
/**
* Toggle option: Insert value.
*/
Invert,
/*
* Add the {value} to a number option, or append the {value} to a string option.
* When the option is a comma separated list, a comma is added, unless the value was empty.
Expand All @@ -27,7 +31,15 @@ export enum SetOptionOperator {
/*
* Subtract the {value} from a number option, or remove the {value} from a string option, if it is there.
*/
Subtract
Subtract,
/**
* Multiply the {value} to a number option, or prepend the {value} to a string option.
*/
Multiply,
/**
* Show value of {option}.
*/
Info
}

export interface IOptionArgs extends node.ICommandArgs {
Expand Down
9 changes: 4 additions & 5 deletions src/cmd_line/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export class Scanner {
}

// Returns the next word in the input, or EOF.
nextWord(): string {
this.skipWhiteSpace();
nextWord(wordSeparators: string[] = [" ", "\t"]): string {
this.skipRun(wordSeparators);

if (this.isAtEof) {
this.pos = this.input.length;
return Scanner.EOF;
Expand All @@ -36,7 +37,7 @@ export class Scanner {
while (!this.isAtEof) {
c = this.next();

if (!(c !== Scanner.EOF && c !== ' ' && c !== '\t')) {
if (c === Scanner.EOF || wordSeparators.indexOf(c) !== -1) {
break;
}

Expand All @@ -47,8 +48,6 @@ export class Scanner {
this.backup();
}

this.pos += result.length;

this.ignore();
return result;
}
Expand Down
64 changes: 44 additions & 20 deletions src/cmd_line/subparsers/setoptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,65 @@ export function parseOption(args: string) : node.IOptionArgs {
return {};
}

let option = scanner.nextWord();
let optionName = scanner.nextWord("?!&=:^+-".split(""));

if (option.startsWith("no")) {
// :se[t] no{option} Toggle option: Reset, switch it off.
if (optionName.startsWith("no")) {
return {
name: option.substring(2, option.length),
name: optionName.substring(2, optionName.length),
operator: node.SetOptionOperator.Reset
};
}

if (option.includes('=')) {
// :se[t] {option}={value} Set string or number option to {value}.
let equalSign = option.indexOf('=');
if (optionName.startsWith("inv")) {
return {
name: option.substring(0, equalSign),
value: option.substring(equalSign + 1, option.length),
operator: node.SetOptionOperator.Equal
name: optionName.substring(3, optionName.length),
operator: node.SetOptionOperator.Invert
};
}

if (option.includes(':')) {
// :se[t] {option}:{value} Set string or number option to {value}.
let equalSign = option.indexOf(':');
scanner.skipWhiteSpace();

if (scanner.isAtEof) {
return {
name: option.substring(0, equalSign),
value: option.substring(equalSign + 1, option.length),
operator: node.SetOptionOperator.Equal
name: optionName,
operator: node.SetOptionOperator.Set
};
}

// :se[t] {option}
return {
name: option,
operator: node.SetOptionOperator.Set
let operator = scanner.next();
let optionArgs: node.IOptionArgs = {
name: optionName,
value: scanner.nextWord([])
};

switch (operator) {
case "=":
case ":":
optionArgs.operator = node.SetOptionOperator.Equal;
break;
case "!":
optionArgs.operator = node.SetOptionOperator.Invert;
break;
case "^":
optionArgs.operator = node.SetOptionOperator.Multiply;
break;
case "+":
optionArgs.operator = node.SetOptionOperator.Append;
break;
case "-":
optionArgs.operator = node.SetOptionOperator.Subtract;
break;
case "?":
optionArgs.operator = node.SetOptionOperator.Info;
break;
case "&":
optionArgs.operator = node.SetOptionOperator.Reset;
break;
default:
throw new Error("Unknown option");
}

return optionArgs;
}

export function parseOptionsCommandArgs(args : string) : node.SetOptionsCommand {
Expand Down
Loading

0 comments on commit 08783b4

Please sign in to comment.