-
Notifications
You must be signed in to change notification settings - Fork 131
/
did_change.go
107 lines (93 loc) · 2.82 KB
/
did_change.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package handlers
import (
"context"
"fmt"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/langserver/diagnostics"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
)
func TextDocumentDidChange(ctx context.Context, params lsp.DidChangeTextDocumentParams) error {
p := lsp.DidChangeTextDocumentParams{
TextDocument: lsp.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: lsp.TextDocumentIdentifier{
URI: params.TextDocument.URI,
},
Version: params.TextDocument.Version,
},
ContentChanges: params.ContentChanges,
}
fs, err := lsctx.DocumentStorage(ctx)
if err != nil {
return err
}
fh := ilsp.VersionedFileHandler(p.TextDocument)
f, err := fs.GetDocument(fh)
if err != nil {
return err
}
// Versions don't have to be consecutive, but they must be increasing
if int(p.TextDocument.Version) <= f.Version() {
fs.CloseAndRemoveDocument(fh)
return fmt.Errorf("Old version (%d) received, current version is %d. "+
"Unable to update %s. This is likely a bug, please report it.",
int(p.TextDocument.Version), f.Version(), p.TextDocument.URI)
}
changes, err := ilsp.DocumentChanges(params.ContentChanges, f)
if err != nil {
return err
}
err = fs.ChangeDocument(fh, changes)
if err != nil {
return err
}
modMgr, err := lsctx.ModuleManager(ctx)
if err != nil {
return err
}
mod, err := modMgr.ModuleByPath(fh.Dir())
if err != nil {
return err
}
err = modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeParseModuleConfiguration)
if err != nil {
return err
}
err = modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeParseVariables)
if err != nil {
return err
}
// TODO: parallelise the operations below in a workgroup
err = modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeLoadModuleMetadata)
if err != nil {
return err
}
err = modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeDecodeReferenceTargets)
if err != nil {
return err
}
err = modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeDecodeReferenceOrigins)
if err != nil {
return err
}
notifier, err := lsctx.DiagnosticsNotifier(ctx)
if err != nil {
return err
}
// obtain fresh module state after the above operations finished
mod, err = modMgr.ModuleByPath(fh.Dir())
if err != nil {
return err
}
diags := diagnostics.NewDiagnostics()
diags.EmptyRootDiagnostic()
diags.Append("HCL", mod.ModuleDiagnostics.AsMap())
diags.Append("HCL", mod.VarsDiagnostics.AutoloadedOnly().AsMap())
if vf, ok := ast.NewVarsFilename(f.Filename()); ok && !vf.IsAutoloaded() {
diags.Append("HCL", mod.VarsDiagnostics.ForFile(vf).AsMap())
}
notifier.PublishHCLDiags(ctx, mod.Path, diags)
return nil
}