Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp/completions: Implement a minimal completions provider #709

Merged
merged 8 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 15 additions & 31 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,43 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell v1.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect
)

require (
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/fgprof v0.9.3 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell v1.1.4 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/flatbuffers v24.3.25+incompatible // indirect
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.51.1 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
// wasip1 support isn't in a tagged version yet, remove for v1.9.4
github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yashtewari/glob-intersection v0.2.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
104 changes: 48 additions & 56 deletions go.sum

Large diffs are not rendered by default.

240 changes: 0 additions & 240 deletions internal/lsp/cache.go
Original file line number Diff line number Diff line change
@@ -1,241 +1 @@
package lsp

import (
"fmt"
"os"
"sync"

"github.com/open-policy-agent/opa/ast"

"github.com/styrainc/regal/internal/lsp/types"
)

// Cache is used to store: current file contents (which includes unsaved changes), the latest parsed modules, and
// diagnostics for each file (including diagnostics gathered from linting files alongside other files).
type Cache struct {
// fileContents is a map of file URI to raw file contents received from the client
fileContents map[string]string
fileContentsMu sync.Mutex

// modules is a map of file URI to parsed AST modules from the latest file contents value
modules map[string]*ast.Module
moduleMu sync.Mutex

// diagnosticsFile is a map of file URI to diagnostics for that file
diagnosticsFile map[string][]types.Diagnostic
diagnosticsFileMu sync.Mutex

// diagnosticsAggregate is a map of file URI to aggregate diagnostics for that file
diagnosticsAggregate map[string][]types.Diagnostic
diagnosticsAggregateMu sync.Mutex

// diagnosticsParseErrors is a map of file URI to parse errors for that file
diagnosticsParseErrors map[string][]types.Diagnostic
diagnosticsParseMu sync.Mutex

builtinPositionsFile map[string]map[uint][]BuiltinPosition
builtinPositionsMu sync.Mutex
}

func NewCache() *Cache {
return &Cache{
fileContents: make(map[string]string),
modules: make(map[string]*ast.Module),

diagnosticsFile: make(map[string][]types.Diagnostic),
diagnosticsAggregate: make(map[string][]types.Diagnostic),
diagnosticsParseErrors: make(map[string][]types.Diagnostic),

builtinPositionsFile: make(map[string]map[uint][]BuiltinPosition),
}
}

func (c *Cache) GetAllDiagnosticsForURI(uri string) []types.Diagnostic {
parseDiags, ok := c.GetParseErrors(uri)
if ok && len(parseDiags) > 0 {
return parseDiags
}

allDiags := make([]types.Diagnostic, 0)

aggDiags, ok := c.GetAggregateDiagnostics(uri)
if ok {
allDiags = append(allDiags, aggDiags...)
}

fileDiags, ok := c.GetFileDiagnostics(uri)
if ok {
allDiags = append(allDiags, fileDiags...)
}

return allDiags
}

func (c *Cache) GetAllFiles() map[string]string {
c.fileContentsMu.Lock()
defer c.fileContentsMu.Unlock()

return c.fileContents
}

func (c *Cache) GetFileContents(uri string) (string, bool) {
c.fileContentsMu.Lock()
defer c.fileContentsMu.Unlock()

val, ok := c.fileContents[uri]

return val, ok
}

func (c *Cache) SetFileContents(uri string, content string) {
c.fileContentsMu.Lock()
defer c.fileContentsMu.Unlock()

c.fileContents[uri] = content
}

func (c *Cache) GetAllModules() map[string]*ast.Module {
c.moduleMu.Lock()
defer c.moduleMu.Unlock()

return c.modules
}

func (c *Cache) GetModule(uri string) (*ast.Module, bool) {
c.moduleMu.Lock()
defer c.moduleMu.Unlock()

val, ok := c.modules[uri]

return val, ok
}

func (c *Cache) SetModule(uri string, module *ast.Module) {
c.moduleMu.Lock()
defer c.moduleMu.Unlock()

c.modules[uri] = module
}

func (c *Cache) GetFileDiagnostics(uri string) ([]types.Diagnostic, bool) {
c.diagnosticsFileMu.Lock()
defer c.diagnosticsFileMu.Unlock()

val, ok := c.diagnosticsFile[uri]

return val, ok
}

func (c *Cache) SetFileDiagnostics(uri string, diags []types.Diagnostic) {
c.diagnosticsFileMu.Lock()
defer c.diagnosticsFileMu.Unlock()

c.diagnosticsFile[uri] = diags
}

func (c *Cache) ClearFileDiagnostics() {
c.diagnosticsFileMu.Lock()
defer c.diagnosticsFileMu.Unlock()

c.diagnosticsFile = make(map[string][]types.Diagnostic)
}

func (c *Cache) GetAggregateDiagnostics(uri string) ([]types.Diagnostic, bool) {
c.diagnosticsAggregateMu.Lock()
defer c.diagnosticsAggregateMu.Unlock()

val, ok := c.diagnosticsAggregate[uri]

return val, ok
}

func (c *Cache) SetAggregateDiagnostics(uri string, diags []types.Diagnostic) {
c.diagnosticsAggregateMu.Lock()
defer c.diagnosticsAggregateMu.Unlock()

c.diagnosticsAggregate[uri] = diags
}

func (c *Cache) ClearAggregateDiagnostics() {
c.diagnosticsAggregateMu.Lock()
defer c.diagnosticsAggregateMu.Unlock()

c.diagnosticsAggregate = make(map[string][]types.Diagnostic)
}

func (c *Cache) GetParseErrors(uri string) ([]types.Diagnostic, bool) {
c.diagnosticsParseMu.Lock()
defer c.diagnosticsParseMu.Unlock()

val, ok := c.diagnosticsParseErrors[uri]

return val, ok
}

func (c *Cache) SetParseErrors(uri string, diags []types.Diagnostic) {
c.diagnosticsParseMu.Lock()
defer c.diagnosticsParseMu.Unlock()

c.diagnosticsParseErrors[uri] = diags
}

func (c *Cache) GetBuiltinPositions(uri string) (map[uint][]BuiltinPosition, bool) {
c.builtinPositionsMu.Lock()
defer c.builtinPositionsMu.Unlock()

val, ok := c.builtinPositionsFile[uri]

return val, ok
}

func (c *Cache) SetBuiltinPositions(uri string, positions map[uint][]BuiltinPosition) {
c.builtinPositionsMu.Lock()
defer c.builtinPositionsMu.Unlock()

c.builtinPositionsFile[uri] = positions
}

// Delete removes all cached data for a given URI.
func (c *Cache) Delete(uri string) {
c.fileContentsMu.Lock()
delete(c.fileContents, uri)
c.fileContentsMu.Unlock()

c.moduleMu.Lock()
delete(c.modules, uri)
c.moduleMu.Unlock()

c.diagnosticsFileMu.Lock()
delete(c.diagnosticsFile, uri)
c.diagnosticsFileMu.Unlock()

c.diagnosticsAggregateMu.Lock()
delete(c.diagnosticsAggregate, uri)
c.diagnosticsAggregateMu.Unlock()

c.diagnosticsParseMu.Lock()
delete(c.diagnosticsParseErrors, uri)
c.diagnosticsParseMu.Unlock()

c.builtinPositionsMu.Lock()
delete(c.builtinPositionsFile, uri)
c.builtinPositionsMu.Unlock()
}

func updateCacheForURIFromDisk(cache *Cache, uri, path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}

currentContent := string(content)

cachedContent, ok := cache.GetFileContents(uri)
if ok && cachedContent == currentContent {
return cachedContent, nil
}

cache.SetFileContents(uri, currentContent)

return currentContent, nil
}