-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: relative, plus/minus ranges. closes #2384 #3071
Merged
xconverge
merged 11 commits into
VSCodeVim:master
from
captaincaius:relative-command-ranges
Oct 11, 2018
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d16d44f
feature: relative, plus/minus ranges. closes #2384
captaincaius b3c3075
Merge branch 'master' of https://github.com/captaincaius/Vim into rel…
captaincaius 1bf8bb1
command lexer refactor so LineNumber and Offset are distinct
captaincaius 2397fba
Merge branch 'master' of https://github.com/VSCodeVim/Vim into relati…
captaincaius e1bc34a
Dependent feature added: numLines-colon shorthand
captaincaius 991696b
Merge branch 'master' into relative-command-ranges
xconverge 05b604b
Merge branch 'master' of https://github.com/VSCodeVim/Vim into relati…
captaincaius e07f0b2
lets to consts
captaincaius e742c57
fix - exotic count-related bugs along with tests
captaincaius 74f13b5
Merge branch 'relative-command-ranges' of https://github.com/captainc…
captaincaius 340452a
Merge branch 'master' into relative-command-ranges
captaincaius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ import * as vscode from 'vscode'; | |
import { VimState } from '../state/vimState'; | ||
import * as token from './token'; | ||
|
||
type LineRefOperation = token.TokenType.Plus | token.TokenType.Minus | undefined; | ||
|
||
export class LineRange { | ||
left: token.Token[]; | ||
separator: token.Token; | ||
|
@@ -20,15 +22,27 @@ export class LineRange { | |
} | ||
|
||
if (!this.separator) { | ||
if (this.left.length > 0 && tok.type !== token.TokenType.Offset) { | ||
// XXX: is this always this error? | ||
throw Error('not a Vim command'); | ||
if (this.left.length > 0) { | ||
switch (tok.type) { | ||
case token.TokenType.Offset: | ||
case token.TokenType.Plus: | ||
case token.TokenType.Minus: | ||
break; | ||
default: | ||
throw Error('Trailing characters'); | ||
} | ||
} | ||
this.left.push(tok); | ||
} else { | ||
if (this.right.length > 0 && tok.type !== token.TokenType.Offset) { | ||
// XXX: is this always this error? | ||
throw Error('not a Vim command'); | ||
if (this.right.length > 0) { | ||
switch (tok.type) { | ||
case token.TokenType.Offset: | ||
case token.TokenType.Plus: | ||
case token.TokenType.Minus: | ||
break; | ||
default: | ||
throw Error('Trailing characters'); | ||
} | ||
} | ||
this.right.push(tok); | ||
} | ||
|
@@ -57,20 +71,30 @@ export class LineRange { | |
toks: token.Token[], | ||
vimState: VimState | ||
): vscode.Position { | ||
var first = toks[0]; | ||
switch (first.type) { | ||
case token.TokenType.Dollar: | ||
let currentLineNum: number; | ||
let currentColumn = 0; // only mark does this differently | ||
let currentOperation: LineRefOperation = undefined; | ||
|
||
var firstToken = toks[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
// handle first-token special cases (e.g. %, inital line number is "." by default) | ||
switch (firstToken.type) { | ||
case token.TokenType.Percent: | ||
return new vscode.Position(doc.document.lineCount - 1, 0); | ||
case token.TokenType.Dollar: | ||
currentLineNum = doc.document.lineCount - 1; | ||
break; | ||
case token.TokenType.Plus: | ||
case token.TokenType.Minus: | ||
case token.TokenType.Dot: | ||
return new vscode.Position(doc.selection.active.line, 0); | ||
currentLineNum = doc.selection.active.line; | ||
// undocumented: if the first token is plus or minus, vim seems to behave as though there was a "." | ||
currentOperation = firstToken.type === token.TokenType.Dot ? undefined : firstToken.type; | ||
break; | ||
case token.TokenType.LineNumber: | ||
var line = Number.parseInt(first.content, 10); | ||
line = Math.max(0, line - 1); | ||
line = Math.min(doc.document.lineCount, line); | ||
return new vscode.Position(line, 0); | ||
currentLineNum = Number.parseInt(firstToken.content, 10) - 1; // user sees 1-based - everything else is 0-based | ||
break; | ||
case token.TokenType.SelectionFirstLine: | ||
let startLine = Math.min.apply( | ||
currentLineNum = Math.min.apply( | ||
null, | ||
doc.selections.map( | ||
selection => | ||
|
@@ -79,21 +103,94 @@ export class LineRange { | |
: selection.end.line | ||
) | ||
); | ||
return new vscode.Position(startLine, 0); | ||
break; | ||
case token.TokenType.SelectionLastLine: | ||
let endLine = Math.max.apply( | ||
currentLineNum = Math.max.apply( | ||
null, | ||
doc.selections.map( | ||
selection => | ||
selection.start.isAfter(selection.end) ? selection.start.line : selection.end.line | ||
) | ||
); | ||
return new vscode.Position(endLine, 0); | ||
break; | ||
case token.TokenType.Mark: | ||
return vimState.historyTracker.getMark(first.content).position; | ||
currentLineNum = vimState.historyTracker.getMark(firstToken.content).position.line; | ||
currentColumn = vimState.historyTracker.getMark(firstToken.content).position.character; | ||
break; | ||
default: | ||
throw new Error('Not Implemented'); | ||
} | ||
|
||
// now handle subsequent tokens, offsetting the current candidate line number | ||
for (let tokenIndex = 1; tokenIndex < toks.length; ++tokenIndex) { | ||
let currentToken = toks[tokenIndex]; | ||
|
||
switch (currentOperation) { | ||
case token.TokenType.Plus: | ||
switch (currentToken.type) { | ||
case token.TokenType.Minus: | ||
case token.TokenType.Plus: | ||
// undocumented: when there's two operators in a row, vim behaves as though there's a "1" between them | ||
currentLineNum += 1; | ||
currentColumn = 0; | ||
currentOperation = currentToken.type; | ||
break; | ||
case token.TokenType.Offset: | ||
currentLineNum += Number.parseInt(currentToken.content, 10); | ||
currentColumn = 0; | ||
currentOperation = undefined; | ||
break; | ||
default: | ||
throw Error('Trailing characters'); | ||
} | ||
break; | ||
case token.TokenType.Minus: | ||
switch (currentToken.type) { | ||
case token.TokenType.Minus: | ||
case token.TokenType.Plus: | ||
// undocumented: when there's two operators in a row, vim behaves as though there's a "1" between them | ||
currentLineNum -= 1; | ||
currentColumn = 0; | ||
currentOperation = currentToken.type; | ||
break; | ||
case token.TokenType.Offset: | ||
currentLineNum -= Number.parseInt(currentToken.content, 10); | ||
currentColumn = 0; | ||
currentOperation = undefined; | ||
break; | ||
default: | ||
throw Error('Trailing characters'); | ||
} | ||
break; | ||
case undefined: | ||
switch (currentToken.type) { | ||
case token.TokenType.Minus: | ||
case token.TokenType.Plus: | ||
currentOperation = currentToken.type; | ||
break; | ||
default: | ||
throw Error('Trailing characters'); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
// undocumented: when there's a trailing operation in the tank without an RHS, vim uses "1" | ||
switch (currentOperation) { | ||
case token.TokenType.Plus: | ||
currentLineNum += 1; | ||
currentColumn = 0; | ||
break; | ||
case token.TokenType.Minus: | ||
currentLineNum -= 1; | ||
currentColumn = 0; | ||
break; | ||
} | ||
|
||
// finally, make sure current position is in bounds :) | ||
currentLineNum = Math.max(0, currentLineNum); | ||
currentLineNum = Math.min(doc.document.lineCount - 1, currentLineNum); | ||
return new vscode.Position(currentLineNum, currentColumn); | ||
xconverge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done