-
Notifications
You must be signed in to change notification settings - Fork 131
/
did_open.go
72 lines (61 loc) · 1.99 KB
/
did_open.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
72
package handlers
import (
"context"
"fmt"
"github.com/creachadair/jrpc2"
"github.com/hashicorp/terraform-ls/internal/document"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/uri"
)
func (svc *service) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpenTextDocumentParams) error {
docURI := string(params.TextDocument.URI)
// URIs are always checked during initialize request, but
// we still allow single-file mode, therefore invalid URIs
// can still land here, so we check for those.
if !uri.IsURIValid(docURI) {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Type: lsp.Warning,
Message: fmt.Sprintf("Ignoring workspace folder (unsupport or invalid URI) %s."+
" This is most likely bug, please report it.", docURI),
})
return fmt.Errorf("invalid URI: %s", docURI)
}
dh := document.HandleFromURI(docURI)
err := svc.stateStore.DocumentStore.OpenDocument(dh, params.TextDocument.LanguageID,
int(params.TextDocument.Version), []byte(params.TextDocument.Text))
if err != nil {
return err
}
mod, err := svc.modStore.ModuleByPath(dh.Dir.Path())
if err != nil {
if state.IsModuleNotFound(err) {
err = svc.modStore.Add(dh.Dir.Path())
if err != nil {
return err
}
mod, err = svc.modStore.ModuleByPath(dh.Dir.Path())
if err != nil {
return err
}
} else {
return err
}
}
svc.logger.Printf("opened module: %s", mod.Path)
// We reparse because the file being opened may not match
// (originally parsed) content on the disk
// TODO: Do this only if we can verify the file differs?
modHandle := document.DirHandleFromPath(mod.Path)
jobIds, err := svc.indexer.DocumentOpened(modHandle)
if err != nil {
return err
}
if svc.singleFileMode {
err = svc.stateStore.WalkerPaths.EnqueueDir(modHandle)
if err != nil {
return err
}
}
return svc.stateStore.JobStore.WaitForJobs(ctx, jobIds...)
}