-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tell LSP server about new, del, get, and put events in acme/log
Before we were only handling put for formatting. Now we handle all events related to file synchronization. We also send all open files to LSP server on startup. Helps #6
- Loading branch information
Showing
6 changed files
with
210 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package acmelsp | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"9fans.net/go/acme" | ||
"github.com/fhs/acme-lsp/internal/acmeutil" | ||
"github.com/fhs/acme-lsp/internal/lsp/client" | ||
"github.com/fhs/acme-lsp/internal/lsp/text" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type FileManager struct { | ||
ss *client.ServerSet | ||
wins map[string]struct{} // set of open files | ||
mu sync.Mutex | ||
} | ||
|
||
func NewFileManager(ss *client.ServerSet) (*FileManager, error) { | ||
fm := &FileManager{ | ||
ss: ss, | ||
wins: make(map[string]struct{}), | ||
} | ||
|
||
wins, err := acme.Windows() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to read list of acme index") | ||
} | ||
for _, info := range wins { | ||
err := fm.DidOpen(info.ID, info.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return fm, nil | ||
} | ||
|
||
func (fm *FileManager) withClient(winid int, name string, f func(*client.Conn, *acmeutil.Win) error) error { | ||
s, found, err := fm.ss.StartForFile(name) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
return nil // Unknown language server. | ||
} | ||
|
||
var win *acmeutil.Win | ||
if winid >= 0 { | ||
w, err := acmeutil.OpenWin(winid) | ||
if err != nil { | ||
return err | ||
} | ||
defer w.CloseFiles() | ||
win = w | ||
} | ||
return f(s.Conn, win) | ||
} | ||
|
||
func (fm *FileManager) DidOpen(winid int, name string) error { | ||
return fm.withClient(winid, name, func(c *client.Conn, w *acmeutil.Win) error { | ||
fm.mu.Lock() | ||
defer fm.mu.Unlock() | ||
|
||
if _, ok := fm.wins[name]; ok { | ||
return fmt.Errorf("file already open in file manager: %v", name) | ||
} | ||
fm.wins[name] = struct{}{} | ||
|
||
b, err := w.ReadAll("body") | ||
if err != nil { | ||
return err | ||
} | ||
return c.DidOpen(name, b) | ||
}) | ||
} | ||
|
||
func (fm *FileManager) DidClose(name string) error { | ||
fm.mu.Lock() | ||
defer fm.mu.Unlock() | ||
|
||
if _, ok := fm.wins[name]; !ok { | ||
return nil // Unknown language server. | ||
} | ||
delete(fm.wins, name) | ||
|
||
return fm.withClient(-1, name, func(c *client.Conn, _ *acmeutil.Win) error { | ||
return c.DidClose(name) | ||
}) | ||
} | ||
|
||
func (fm *FileManager) DidChange(winid int, name string) error { | ||
fm.mu.Lock() | ||
defer fm.mu.Unlock() | ||
|
||
if _, ok := fm.wins[name]; !ok { | ||
return nil // Unknown language server. | ||
} | ||
return fm.withClient(winid, name, func(c *client.Conn, w *acmeutil.Win) error { | ||
b, err := w.ReadAll("body") | ||
if err != nil { | ||
return err | ||
} | ||
return c.DidChange(name, b) | ||
}) | ||
} | ||
|
||
func (fm *FileManager) DidSave(winid int, name string) error { | ||
fm.mu.Lock() | ||
defer fm.mu.Unlock() | ||
|
||
if _, ok := fm.wins[name]; !ok { | ||
return nil // Unknown language server. | ||
} | ||
return fm.withClient(winid, name, func(c *client.Conn, w *acmeutil.Win) error { | ||
b, err := w.ReadAll("body") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO(fhs): Maybe DidChange is not needed with includeText option to DidSave? | ||
err = c.DidChange(name, b) | ||
if err != nil { | ||
return err | ||
} | ||
return c.DidSave(name) | ||
}) | ||
} | ||
|
||
func (fm *FileManager) Format(winid int, name string) error { | ||
fm.mu.Lock() | ||
defer fm.mu.Unlock() | ||
|
||
if _, ok := fm.wins[name]; !ok { | ||
return nil // Unknown language server. | ||
} | ||
return fm.withClient(winid, name, func(c *client.Conn, w *acmeutil.Win) error { | ||
return FormatFile(c, text.ToURI(name), w) | ||
}) | ||
} |
Oops, something went wrong.