]> Sergey Matveev's repositories - path-extractor.git/blob - extractor.go
Tests workability
[path-extractor.git] / extractor.go
1 package pathextractor
2
3 import "regexp"
4 import "strings"
5 import "fmt"
6
7 type MatchOptions struct {
8         format string
9 }
10
11 func pathExtractor(input string) [][]int {
12         surroundRegex := "[@~\\-_a-zA-ZА-Яа-яЁё/.0-9]*"
13         r := regexp.MustCompile("(" + surroundRegex + "[\\./]" + surroundRegex + ")")
14         return r.FindAllSubmatchIndex([]byte(input), -1)
15 }
16
17 func stripParens(input string) string {
18         r := regexp.MustCompile("^\\((.*)\\)$")
19         temp := [][]byte{}
20         temp = r.FindSubmatch([]byte(input))
21         if len(temp) <= 1 {
22                 return input
23         }
24         return string(temp[1])
25 }
26
27 func postProcess(input string) string {
28         input = stripParens(input)
29         return input
30 }
31
32 func GetAllMatches(input string, format string) []string {
33         options := MatchOptions{format: format}
34         result := []string{}
35         candidatePath := string("")
36         restOfLine := string("")
37         indexes := pathExtractor(input)
38         for _, index := range indexes {
39                 candidatePath = input[index[0]:index[1]]
40                 if len(input) >= len(candidatePath+"(") && strings.Index(input, candidatePath+"(") != -1 {
41                         continue
42                 }
43
44                 if isIp(candidatePath) || isEmail(candidatePath) || isDate(candidatePath) || isVersion(candidatePath) || isGitRange(candidatePath) || isGitInstruction(candidatePath) || startsWithInvalidString(candidatePath) || endsWithInvalidString(candidatePath) || containsInvalidString(candidatePath) || len(candidatePath) <= 2 || isSpace(candidatePath) {
45                         continue
46                 }
47                 if isGitPath(candidatePath) {
48                         candidatePath = replaceGitPath(candidatePath)
49                 }
50                 candidatePath = postProcess(candidatePath)
51                 if options.format == "ackmate" {
52                         restOfLine = input[index[1]:]
53                         cursorPos := getCursorPosition(restOfLine)
54                         candidatePath = fmt.Sprint(candidatePath, cursorPos)
55                 }
56                 result = append(result, candidatePath)
57         }
58         return result
59 }
60
61 func getCursorPosition(input string) string {
62         r := regexp.MustCompile("^(:[0-9]+(:[0-9]+)?)")
63         temp := [][]byte{}
64         temp = r.FindSubmatch([]byte(input))
65         if len(temp) <= 1 {
66                 return ""
67         }
68         return string(temp[1])
69 }