mirror of
https://github.com/golang/go
synced 2024-11-19 01:34:40 -07:00
8deeabbe2e
Refactor code a bit to support range formatting as well document formatting. Also, separate view from server to clean up. Change-Id: Ica397c7a0fb92a7708ea247c2d5de83e5528d8d4 Reviewed-on: https://go-review.googlesource.com/138275 Reviewed-by: Alan Donovan <adonovan@google.com>
43 lines
823 B
Go
43 lines
823 B
Go
package lsp
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
)
|
|
|
|
type view struct {
|
|
activeFilesMu sync.Mutex
|
|
activeFiles map[protocol.DocumentURI]string
|
|
}
|
|
|
|
func newView() *view {
|
|
return &view{
|
|
activeFiles: make(map[protocol.DocumentURI]string),
|
|
}
|
|
}
|
|
|
|
func (v *view) cacheActiveFile(uri protocol.DocumentURI, text string) {
|
|
v.activeFilesMu.Lock()
|
|
v.activeFiles[uri] = text
|
|
v.activeFilesMu.Unlock()
|
|
}
|
|
|
|
func (v *view) readActiveFile(uri protocol.DocumentURI) (string, error) {
|
|
v.activeFilesMu.Lock()
|
|
defer v.activeFilesMu.Unlock()
|
|
|
|
content, ok := v.activeFiles[uri]
|
|
if !ok {
|
|
return "", fmt.Errorf("file not found: %s", uri)
|
|
}
|
|
return content, nil
|
|
}
|
|
|
|
func (v *view) clearActiveFile(uri protocol.DocumentURI) {
|
|
v.activeFilesMu.Lock()
|
|
delete(v.activeFiles, uri)
|
|
v.activeFilesMu.Unlock()
|
|
}
|