Skip to content

Commit

Permalink
Use TypeScript 2.0 and use strictNullChecks.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfn committed Jul 12, 2016
1 parent 1bc94f4 commit 1463209
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 154 deletions.
4 changes: 2 additions & 2 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let extensionContext: vscode.ExtensionContext;
* see a bug where VSC hasn't fully initialized yet, which pretty much breaks VSCodeVim entirely.
*/
let modeHandlerToFilename: { [key: string]: ModeHandler } = {};
let previousActiveFilename: string = undefined;
let previousActiveFilename: string | undefined = undefined;

let taskQueue = new TaskQueue();

Expand All @@ -27,7 +27,7 @@ function activeFileName(): string {
}

export async function getAndUpdateModeHandler(): Promise<ModeHandler> {
const oldHandler = modeHandlerToFilename[previousActiveFilename];
const oldHandler = previousActiveFilename ? modeHandlerToFilename[previousActiveFilename] : undefined;

if (!modeHandlerToFilename[activeFileName()]) {
const newModeHandler = new ModeHandler(false, activeFileName());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"gulp-typescript": "^2.13.4",
"gulp-typings": "^2.0.0",
"tslint": "^3.10.2",
"typescript": "^1.8.10",
"typescript": "2.0.0",
"typings": "^1.0.4",
"vscode": "^0.11.13"
}
Expand Down
25 changes: 14 additions & 11 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const controlKeys: string[] = [
const compareKeypressSequence = function (one: string[], two: string[]): boolean {
const containsControlKey = (s: string): boolean => {
for (const controlKey of controlKeys) {
if (s.indexOf(controlKey) !== -1) {
if (s.indexOf(controlKey!) !== -1) {
return true;
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ export abstract class BaseMovement extends BaseAction {
*/
public async execActionWithCount(position: Position, vimState: VimState, count: number): Promise<Position | IMovement> {
let recordedState = vimState.recordedState;
let result: Position | IMovement;
let result: Position | IMovement = new Position(0, 0); // bogus init to satisfy typechecker

if (count < 1) {
count = 1;
Expand Down Expand Up @@ -239,7 +239,9 @@ export class BaseOperator extends BaseAction {
/**
* Run this operator on a range, returning the new location of the cursor.
*/
run(vimState: VimState, start: Position, stop: Position): Promise<VimState> { return; }
run(vimState: VimState, start: Position, stop: Position): Promise<VimState> {
throw new Error("You need to override this!");
}
}

export enum KeypressState {
Expand Down Expand Up @@ -268,7 +270,9 @@ export class Actions {
public static getRelevantAction(keysPressed: string[], vimState: VimState): BaseAction | KeypressState {
let couldPotentiallyHaveMatch = false;

for (const { type, action } of Actions.allActions) {
for (const thing of Actions.allActions) {
const { type, action } = thing!;

if (action.doesActionApply(vimState, keysPressed)) {
const result = new type();

Expand Down Expand Up @@ -371,15 +375,14 @@ class CommandInsertInSearchMode extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const key = this.keysPressed[0];

const searchState = vimState.searchState;
const searchState = vimState.searchState!;

// handle special keys first
if (key === "<backspace>") {
searchState.searchString = searchState.searchString.slice(0, -1);
} else if (key === "\n") {
vimState.currentMode = ModeName.Normal;
vimState.cursorPosition = vimState.searchState.getNextSearchMatchPosition(searchState.searchCursorStartPosition).pos;
vimState.cursorPosition = searchState.getNextSearchMatchPosition(searchState.searchCursorStartPosition).pos;

return vimState;
} else if (key === "<esc>") {
Expand Down Expand Up @@ -407,11 +410,11 @@ class CommandNextSearchMatch extends BaseMovement {
public async execAction(position: Position, vimState: VimState): Promise<Position> {
const searchState = vimState.searchState;

if (searchState.searchString === "") {
if (!searchState || searchState.searchString === "") {
return position;
}

return vimState.searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos;
return searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos;
}
}

Expand Down Expand Up @@ -477,7 +480,7 @@ class CommandPreviousSearchMatch extends BaseMovement {
public async execAction(position: Position, vimState: VimState): Promise<Position> {
const searchState = vimState.searchState;

if (searchState.searchString === "") {
if (!searchState || searchState.searchString === "") {
return position;
}

Expand Down Expand Up @@ -1998,7 +2001,7 @@ class MoveToMatchingBracket extends BaseMovement {
*/

let stackHeight = closed ? 0 : 1;
let matchedPosition: Position = undefined;
let matchedPosition: Position | undefined = undefined;

for (const { char, pos } of Position.IterateDocument(position, toFind.nextMatchIsForward)) {
if (char === charToMatch) {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd_line/commands/substitute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ModeHandler } from "../../mode/modeHandler";
import { TextEditor } from "../../textEditor";

export interface ISubstituteCommandArguments extends node.ICommandArgs {
pattern?: string;
replace?: string;
pattern: string;
replace: string;
flags?: number;
count?: number;
}
Expand Down
6 changes: 1 addition & 5 deletions src/cmd_line/commands/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export enum Tab {

export interface ITabCommandArguments extends node.ICommandArgs {
tab: Tab;
count?: number;
count: number;
}

//
Expand All @@ -37,10 +37,6 @@ export class TabCommand extends node.CommandBase {
}

private executeCommandWithCount(count: number, command: string) {
if (!count) {
count = 1;
}

for (let i = 0; i < count; i++) {
vscode.commands.executeCommand(command);
}
Expand Down
41 changes: 22 additions & 19 deletions src/cmd_line/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,47 @@ import {Token, TokenType} from "./token";

// Describes a function that can lex part of a Vim command line.
interface ILexFunction {
(state: Scanner, tokens: Token[]) : ILexFunction;
(state: Scanner, tokens: Token[]) : ILexFunction | null;
}

export function lex(input : string) : Token[] {
// We use a character scanner as state for the lexer.
var state = new Scanner(input);
var tokens : Token[] = [];
var f : ILexFunction = LexerFunctions.lexRange;
var tokens: Token[] = [];
var f: ILexFunction | null = LexerFunctions.lexRange;
while (f) {
// Each lexing function returns the next lexing function or null.
f = f(state, tokens);
}
return tokens;
}

function emitToken(type : TokenType, state : Scanner) : Token {
function emitToken(type: TokenType, state: Scanner): Token | null {
var content = state.emit();

return (content.length > 0) ? new Token(type, content) : null;
}

module LexerFunctions {
// Starts lexing a Vim command line and delegates on other lexer functions as needed.
export function lexRange(state : Scanner, tokens : Token[]): ILexFunction {
export function lexRange(state: Scanner, tokens: Token[]): ILexFunction {
while (true) {
if (state.isAtEof) {
break;
}
var c = state.next();
switch (c) {
case ",":
tokens.push(emitToken(TokenType.Comma, state));
tokens.push(emitToken(TokenType.Comma, state)!);
continue;
case "%":
tokens.push(emitToken(TokenType.Percent, state));
tokens.push(emitToken(TokenType.Percent, state)!);
continue;
case "$":
tokens.push(emitToken(TokenType.Dollar, state));
tokens.push(emitToken(TokenType.Dollar, state)!);
continue;
case ".":
tokens.push(emitToken(TokenType.Dot, state));
tokens.push(emitToken(TokenType.Dot, state)!);
continue;
case "/":
return lexForwardSearch;
Expand All @@ -62,25 +63,27 @@ module LexerFunctions {
case "9":
return lexLineRef;
case "+":
tokens.push(emitToken(TokenType.Plus, state));
tokens.push(emitToken(TokenType.Plus, state)!);
continue;
case "-":
tokens.push(emitToken(TokenType.Minus, state));
tokens.push(emitToken(TokenType.Minus, state)!);
continue;
default:
return lexCommand;
}
}

return null;
}

function lexLineRef(state : Scanner, tokens : Token[]): ILexFunction {
function lexLineRef(state : Scanner, tokens: Token[]): ILexFunction | null {
// The first digit has already been lexed.
while (true) {
if (state.isAtEof) {
tokens.push(emitToken(TokenType.LineNumber, state));
tokens.push(emitToken(TokenType.LineNumber, state)!);
return null;
}

var c = state.next();
switch (c) {
case "0":
Expand All @@ -96,17 +99,17 @@ module LexerFunctions {
continue;
default:
state.backup();
tokens.push(emitToken(TokenType.LineNumber, state));
tokens.push(emitToken(TokenType.LineNumber, state)!);
return lexRange;
}
}
}

function lexCommand(state : Scanner, tokens : Token[]): ILexFunction {
function lexCommand(state : Scanner, tokens : Token[]): ILexFunction | null {
// The first character of the command's name has already been lexed.
while (true) {
if (state.isAtEof) {
tokens.push(emitToken(TokenType.CommandName, state));
tokens.push(emitToken(TokenType.CommandName, state)!);
break;
}
var c = state.next();
Expand All @@ -115,7 +118,7 @@ module LexerFunctions {
continue;
} else {
state.backup();
tokens.push(emitToken(TokenType.CommandName, state));
tokens.push(emitToken(TokenType.CommandName, state)!);
while (!state.isAtEof) {
state.next();
}
Expand All @@ -133,7 +136,7 @@ module LexerFunctions {
function lexForwardSearch(state : Scanner, tokens : Token[]): ILexFunction {
// The first slash has already been lexed.
state.skip("/"); // XXX: really?
var escaping : boolean;
var escaping = false;
var searchTerm = "";
while (!state.isAtEof) {
var c = state.next();
Expand All @@ -159,7 +162,7 @@ module LexerFunctions {
function lexReverseSearch(state : Scanner, tokens : Token[]): ILexFunction {
// The first question mark has already been lexed.
state.skip("?"); // XXX: really?
var escaping : boolean;
var escaping = false;
var searchTerm = "";
while (!state.isAtEof) {
var c = state.next();
Expand Down
4 changes: 2 additions & 2 deletions src/cmd_line/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as parser from "./parser";
import {ModeHandler} from "../mode/modeHandler";

// Shows the vim command line.
export async function showCmdLine(initialText: string, modeHandler : ModeHandler): Promise<{}> {
export async function showCmdLine(initialText: string, modeHandler : ModeHandler): Promise<undefined> {
if (!vscode.window.activeTextEditor) {
console.log("No active document.");
return;
Expand All @@ -23,7 +23,7 @@ export async function showCmdLine(initialText: string, modeHandler : ModeHandler
}
}

export async function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> {
export async function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<undefined> {
if (!command || command.length === 0) {
return;
}
Expand Down
9 changes: 5 additions & 4 deletions src/cmd_line/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import * as lexer from './lexer';
import {commandParsers} from './subparser';

interface IParseFunction {
(state : ParserState, command : node.CommandLine) : IParseFunction;
(state : ParserState, command : node.CommandLine): IParseFunction | null;
}

export function parse(input : string) : node.CommandLine {
var cmd = new node.CommandLine();
var f : IParseFunction = parseLineRange;
var f: IParseFunction | null = parseLineRange;
let state : ParserState = new ParserState(input);
while (f) {
f = f(state, cmd);
}
return cmd;
}

function parseLineRange(state : ParserState, commandLine : node.CommandLine) : IParseFunction {
function parseLineRange(state: ParserState, commandLine: node.CommandLine): IParseFunction | null {
while (true) {
let tok = state.next();
switch (tok.type) {
Expand All @@ -44,7 +44,7 @@ function parseLineRange(state : ParserState, commandLine : node.CommandLine) : I
}
}

function parseCommand(state : ParserState, commandLine : node.CommandLine) : IParseFunction {
function parseCommand(state: ParserState, commandLine: node.CommandLine): IParseFunction | null {
while (!state.isAtEof) {
var tok = state.next();
switch (tok.type) {
Expand All @@ -63,6 +63,7 @@ function parseCommand(state : ParserState, commandLine : node.CommandLine) : IPa
throw new Error("Not implemented");
}
}

if (!state.isAtEof) {
state.backup();
return parseCommand;
Expand Down
20 changes: 12 additions & 8 deletions src/cmd_line/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ export class Scanner {
}

// Returns the next word in the input, or EOF.
nextWord() : string {
nextWord(): string {
this.skipWhiteSpace();
if (this.isAtEof) {
this.pos = this.input.length;
return Scanner.EOF;
}

let result = "";
let c: string | undefined = undefined;

while (!this.isAtEof) {
var c = this.next();
if (c !== Scanner.EOF && c !== ' ' && c !== '\t') {
result += c;
continue;
} else {
c = this.next();

if (!(c !== Scanner.EOF && c !== ' ' && c !== '\t')) {
break;
}

result += c;
}

if (c !== Scanner.EOF) {
if (c && c !== Scanner.EOF) {
this.backup();
}

Expand Down Expand Up @@ -109,13 +110,16 @@ export class Scanner {
if (this.isAtEof) {
return;
}
let c: string | null = null;

while (!this.isAtEof) {
var c = this.next();
c = this.next();
if (c === " " || c === "\t") {
continue;
}
break;
}

if (c !== Scanner.EOF && c !== ' ' && c !== '\t') {
this.backup();
}
Expand Down
Loading

0 comments on commit 1463209

Please sign in to comment.