Fix -startpos flag being ignored (#1129)

* Refactored cursor location login into a function. Fixed buffer overflow when line position is 1 more than file lines

* Fixed crash when -startpos has an invalid argument

* Adapted tests to new interface

* Fixed bug where -startpos with lines 0 and 1 would both be on the first line

* Changed Fatalf format back to digits

* Fixed issues with buffer cursor location. Added tests for new function

* ParseCursorLocation will now return an error when path doesnt contain line/col

* Fixed off-by-one line error

* Fixed tests to account for subtracting 1 from the line index
This commit is contained in:
Dimitar Borislavov Tasev
2018-06-04 17:27:27 +01:00
committed by Zachary Yedidia
parent 61baa73d70
commit aa74b1233c
4 changed files with 190 additions and 73 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/mattn/go-runewidth"
"regexp"
"github.com/go-errors/errors"
)
// Util.go is a collection of utility functions that are used throughout
@@ -363,9 +364,9 @@ func ReplaceHome(path string) string {
func GetPathAndCursorPosition(path string) (string, []string) {
re := regexp.MustCompile(`([\s\S]+?)(?::(\d+))(?::(\d+))?`)
match := re.FindStringSubmatch(path)
// no lines/columns were specified in the path, return just the path with cursor at 0, 0
// no lines/columns were specified in the path, return just the path with no cursor location
if len(match) == 0 {
return path, []string{"0", "0"}
return path, nil
} else if match[len(match)-1] != "" {
// if the last capture group match isn't empty then both line and column were provided
return match[1], match[2:]
@@ -379,8 +380,8 @@ func ParseCursorLocation(cursorPositions []string) (Loc, error) {
var err error
// if no positions are available exit early
if len(cursorPositions) == 0 {
return startpos, err
if cursorPositions == nil {
return startpos, errors.New("No cursor positions were provided.")
}
startpos.Y, err = strconv.Atoi(cursorPositions[0])