@@ -44,6 +44,7 @@ import (
4444 "github.com/bufbuild/protocompile/experimental/incremental"
4545 "github.com/bufbuild/protocompile/experimental/incremental/queries"
4646 "github.com/bufbuild/protocompile/experimental/ir"
47+ "github.com/bufbuild/protocompile/experimental/report"
4748 "github.com/bufbuild/protocompile/experimental/seq"
4849 "github.com/bufbuild/protocompile/experimental/source"
4950 "go.lsp.dev/protocol"
@@ -63,7 +64,7 @@ type file struct {
6364 uri protocol.URI
6465 checkWork chan <- struct {}
6566
66- text string
67+ file * report. File
6768 // Version is an opaque version identifier given to us by the LSP client. This
6869 // is used in the protocol to disambiguate which version of a file e.g. publishing
6970 // diagnostics or symbols an operating refers to.
@@ -153,13 +154,19 @@ func (f *file) ReadFromDisk(ctx context.Context) (err error) {
153154 return nil
154155 }
155156
156- data , err := os .ReadFile (f .uri .Filename ())
157+ reader , err := os .Open (f .uri .Filename ())
158+ if err != nil {
159+ return fmt .Errorf ("could not open file %q from disk: %w" , f .uri , err )
160+ }
161+ defer reader .Close ()
162+ text , err := readAllAsString (reader )
157163 if err != nil {
158164 return fmt .Errorf ("could not read file %q from disk: %w" , f .uri , err )
159165 }
160166
161167 f .version = - 1
162- f .text = string (data )
168+ f .file = report .NewFile (f .objectInfo .Path (), text )
169+ f .hasText = true
163170 return nil
164171}
165172
@@ -170,7 +177,7 @@ func (f *file) Update(ctx context.Context, version int32, text string) {
170177
171178 f .lsp .logger .Info (fmt .Sprintf ("new file version: %v, %v -> %v" , f .uri , f .version , version ))
172179 f .version = version
173- f .text = text
180+ f .file = report . NewFile ( f . objectInfo . Path (), text )
174181 f .hasText = true
175182}
176183
@@ -435,11 +442,11 @@ func (f *file) RefreshIR(ctx context.Context) {
435442 )
436443
437444 openerMap := map [string ]string {
438- f .objectInfo .Path (): f .text ,
445+ f .objectInfo .Path (): f .file . Text () ,
439446 }
440447 files := []* file {f }
441448 for path , file := range f .importToFile {
442- openerMap [path ] = file .text
449+ openerMap [path ] = file .file . Text ()
443450 files = append (files , file )
444451 }
445452 opener := source .NewMap (openerMap )
@@ -939,7 +946,7 @@ func (f *file) newFileOpener() fileOpener {
939946 if file == nil {
940947 return nil , fmt .Errorf ("%s: %w" , path , fs .ErrNotExist )
941948 }
942- return xio .CompositeReadCloser (strings .NewReader (file .text ), xio .NopCloser ), nil
949+ return xio .CompositeReadCloser (strings .NewReader (file .file . Text () ), xio .NopCloser ), nil
943950 }
944951}
945952
@@ -1095,24 +1102,18 @@ func (f *file) appendLintErrors(source string, err error) bool {
10951102 }
10961103
10971104 for _ , annotation := range annotations .FileAnnotations () {
1105+ // Convert 1-indexed byte-based line/column to byte offset.
1106+ startLocation := f .file .InverseLocation (annotation .StartLine (), annotation .StartColumn (), positionalEncoding )
1107+ endLocation := f .file .InverseLocation (annotation .EndLine (), annotation .EndColumn (), positionalEncoding )
1108+ protocolRange := reportLocationsToProtocolRange (startLocation , endLocation )
10981109 f .diagnostics = append (f .diagnostics , protocol.Diagnostic {
1099- Range : protocol.Range {
1100- Start : protocol.Position {
1101- Line : uint32 (annotation .StartLine ()) - 1 ,
1102- Character : uint32 (annotation .StartColumn ()) - 1 ,
1103- },
1104- End : protocol.Position {
1105- Line : uint32 (annotation .EndLine ()) - 1 ,
1106- Character : uint32 (annotation .EndColumn ()) - 1 ,
1107- },
1108- },
1110+ Range : protocolRange ,
11091111 Code : annotation .Type (),
11101112 Severity : protocol .DiagnosticSeverityWarning ,
11111113 Source : source ,
11121114 Message : annotation .Message (),
11131115 })
11141116 }
1115-
11161117 return true
11171118}
11181119
@@ -1149,3 +1150,11 @@ func (f *file) PublishDiagnostics(ctx context.Context) {
11491150type wktObjectInfo struct {
11501151 storage.ObjectInfo
11511152}
1153+
1154+ func readAllAsString (reader io.Reader ) (string , error ) {
1155+ var builder strings.Builder
1156+ if _ , err := io .Copy (& builder , reader ); err != nil {
1157+ return "" , err
1158+ }
1159+ return builder .String (), nil
1160+ }
0 commit comments