blob: 04bbde6e3676ef1bba18f512087a294f8d9dae7b (
plain) (
blame)
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
|
package location
import (
"regexp"
"github.com/graphql-go/graphql/language/source"
)
type SourceLocation struct {
Line int `json:"line"`
Column int `json:"column"`
}
func GetLocation(s *source.Source, position int) SourceLocation {
body := []byte{}
if s != nil {
body = s.Body
}
line := 1
column := position + 1
lineRegexp := regexp.MustCompile("\r\n|[\n\r]")
matches := lineRegexp.FindAllIndex(body, -1)
for _, match := range matches {
matchIndex := match[0]
if matchIndex < position {
line++
l := len(s.Body[match[0]:match[1]])
column = position + 1 - (matchIndex + l)
continue
} else {
break
}
}
return SourceLocation{Line: line, Column: column}
}
|