-
Notifications
You must be signed in to change notification settings - Fork 5
/
extractor.go
71 lines (63 loc) · 1.92 KB
/
extractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"regexp"
"strings"
)
type MatchOptions struct {
format string
}
func pathExtractor(input string) [][]int {
surroundRegex := "([@~\\-_a-zA-Z/.0-9\u00C0-\u024F\u1E00-\u1EFF])*"
r := regexp.MustCompile("(" + surroundRegex + "[\\./]" + surroundRegex + ")")
return r.FindAllSubmatchIndex([]byte(input), -1)
}
func stripParens(input string) string {
r := regexp.MustCompile("^\\((.*)\\)$")
temp := [][]byte{}
temp = r.FindSubmatch([]byte(input))
if len(temp) <= 1 {
return input
}
return string(temp[1])
}
func postProcess(input string) string {
input = stripParens(input)
return input
}
func GetAllMatches(input string, format string) []string {
options := MatchOptions{format: format}
result := []string{}
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 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(candidatePath) {
candidatePath = replaceGitPath(candidatePath)
}
candidatePath = postProcess(candidatePath)
if options.format == "ackmate" {
restOfLine = input[index[1]:]
cursorPos := getCursorPosition(restOfLine)
candidatePath = fmt.Sprint(candidatePath, cursorPos)
}
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])
}