]> Sergey Matveev's repositories - path-extractor.git/blobdiff - extractor.go
Tests workability
[path-extractor.git] / extractor.go
index a0d43dd822b4561fed3c59ccbb0da5d5c5463d89..3183109418b35e00912f87c1420e5a98e7e0b569 100644 (file)
@@ -8,12 +8,10 @@ type MatchOptions struct {
        format string
 }
 
-func pathExtractor(input string) [][][]byte {
-       surroundRegex := "[^]()!#[ \\t:'\"]*"
+func pathExtractor(input string) [][]int {
+       surroundRegex := "[@~\\-_a-zA-ZА-Яа-яЁё/.0-9]*"
        r := regexp.MustCompile("(" + surroundRegex + "[\\./]" + surroundRegex + ")")
-       temp := [][][]byte{}
-       temp = r.FindAllSubmatch([]byte(input), -1)
-       return temp
+       return r.FindAllSubmatchIndex([]byte(input), -1)
 }
 
 func stripParens(input string) string {
@@ -31,29 +29,41 @@ func postProcess(input string) string {
        return input
 }
 
-func GetAllMatches(input string, options MatchOptions) []string {
-       matches := [][][]byte{}
+func GetAllMatches(input string, format string) []string {
+       options := MatchOptions{format: format}
        result := []string{}
-       s := string("")
-       // print(input)
-       matches = pathExtractor(input)
-       for _, match := range matches {
-               s = string(match[1])
-               if len(input) >= len(s+"(") && strings.Index(input, s+"(") != -1 {
+       candidatePath := string("")
+       restOfLine := string("")
+       indexes := pathExtractor(input)
+       for _, index := range indexes {
+               candidatePath = input[index[0]:index[1]]
+               if len(input) >= len(candidatePath+"(") && strings.Index(input, candidatePath+"(") != -1 {
                        continue
                }
 
-               if isEmail(s) || isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || endsWithInvalidString(s) || containsInvalidString(s) || len(s) <= 2 {
+               if isIp(candidatePath) || isEmail(candidatePath) || isDate(candidatePath) || isVersion(candidatePath) || isGitRange(candidatePath) || isGitInstruction(candidatePath) || startsWithInvalidString(candidatePath) || endsWithInvalidString(candidatePath) || containsInvalidString(candidatePath) || len(candidatePath) <= 2 || isSpace(candidatePath) {
                        continue
                }
-               if isGitPath(s) {
-                       s = replaceGitPath(s)
+               if isGitPath(candidatePath) {
+                       candidatePath = replaceGitPath(candidatePath)
                }
-               s = postProcess(s)
+               candidatePath = postProcess(candidatePath)
                if options.format == "ackmate" {
-                       s = fmt.Sprint(s, ":45")
+                       restOfLine = input[index[1]:]
+                       cursorPos := getCursorPosition(restOfLine)
+                       candidatePath = fmt.Sprint(candidatePath, cursorPos)
                }
-               result = append(result, s)
+               result = append(result, candidatePath)
        }
        return result
 }
+
+func getCursorPosition(input string) string {
+       r := regexp.MustCompile("^(:[0-9]+(:[0-9]+)?)")
+       temp := [][]byte{}
+       temp = r.FindSubmatch([]byte(input))
+       if len(temp) <= 1 {
+               return ""
+       }
+       return string(temp[1])
+}