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

Support for the Python Language Server #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/lsp/acmelsp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func (h *clientHandler) ShowMessage(ctx context.Context, params *protocol.ShowMe
return nil
}

func (h *clientHandler) ShowStatus(ctx context.Context, params *protocol.ShowStatusParams) (*protocol.MessageActionItem, error) {
log.Printf("LSP %v: %v\n", params.Type, params.Message)
return nil, nil
}

func (h *clientHandler) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error {
if h.cfg.Logger != nil {
h.cfg.Logger.Printf("%v: %v\n", params.Type, params.Message)
Expand Down
20 changes: 20 additions & 0 deletions internal/lsp/protocol/tsclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions internal/lsp/protocol/tsprotocol.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/lsp/protocol/tsserver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/lsp/proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (c *lspClientDispatcher) ShowMessage(context.Context, *protocol.ShowMessage
return fmt.Errorf("ShowMessage not implemented")
}

func (c *lspClientDispatcher) ShowStatus(context.Context, *protocol.ShowStatusParams) (*protocol.MessageActionItem,error) {
return nil, fmt.Errorf("ShowStatus not implemented")
}

func (c *lspClientDispatcher) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error {
if Debug {
log.Printf("log: proxy %v: %v\n", params.Type, params.Message)
Expand Down
154 changes: 154 additions & 0 deletions internal/lsp/text/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
package text

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"

"github.com/fhs/acme-lsp/internal/golang_org_x_tools/span"
"github.com/fhs/acme-lsp/internal/lsp/protocol"
Expand All @@ -29,6 +36,7 @@ func Edit(f File, edits []protocol.TextEdit) error {
if len(edits) == 0 {
return nil
}

reader, err := f.Reader()
if err != nil {
return err
Expand All @@ -38,6 +46,21 @@ func Edit(f File, edits []protocol.TextEdit) error {
return fmt.Errorf("failed to obtain newline offsets: %v", err)
}

if span := edits[0].Range; len(edits) == 1 && span.Start.Line == 0 && span.Start.Character == 0 && int(span.End.Line) >= len(off.nl)-1 {
reader, err := f.Reader()
if err != nil {
return err
}
p, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
edits, err = diffEdits(p, []byte(edits[0].NewText))
if err != nil {
return err
}
}

f.DisableMark()
f.Mark()

Expand Down Expand Up @@ -111,3 +134,134 @@ func ToURI(filename string) protocol.DocumentURI {
func ToPath(uri protocol.DocumentURI) string {
return span.NewURI(uri).Filename()
}

func diffEdits(old, new []byte) ([]protocol.TextEdit, error) {
oldTemp, err := tempfile(old)
if err != nil {
return nil, err
}
defer os.Remove(oldTemp)
newTemp, err := tempfile(new)
if err != nil {
return nil, err
}
defer os.Remove(newTemp)

diff, _ := exec.Command("9", "diff", oldTemp, newTemp).CombinedOutput()

var diffs []protocol.TextEdit

// Adapted from acmego.
diffLines := strings.Split(string(diff), "\n")
for _, line := range diffLines {
if line == "" {
continue
}
if line[0] == '<' || line[0] == '-' || line[0] == '>' {
continue
}
j := 0
for j < len(line) && line[j] != 'a' && line[j] != 'c' && line[j] != 'd' {
j++
}
if j >= len(line) {
return nil, fmt.Errorf("cannot parse diff line: %q", line)
}
oldStart, oldEnd := parseSpan(line[:j])
newStart, newEnd := parseSpan(line[j+1:])
if newStart == 0 || (oldStart == 0 && line[j] != 'a') {
continue
}
switch line[j] {
case 'a':
diffs = append(diffs, protocol.TextEdit{
Range: protocol.Range{
Start: protocol.Position{Line: float64(oldStart - 1)},
End: protocol.Position{Line: float64(oldStart - 1)},
},
NewText: string(findLines(new, newStart, newEnd)),
})
case 'c':
diffs = append(diffs, protocol.TextEdit{
Range: protocol.Range{
Start: protocol.Position{Line: float64(oldStart - 1)},
End: protocol.Position{Line: float64(oldEnd)},
},
NewText: string(findLines(new, newStart, newEnd)),
})
case 'd':
diffs = append(diffs, protocol.TextEdit{
Range: protocol.Range{
Start: protocol.Position{Line: float64(oldStart - 1)},
End: protocol.Position{Line: float64(oldEnd - 1)},
},
NewText: "",
})
}
}
if !bytes.HasSuffix(old, nlBytes) && bytes.HasSuffix(new, nlBytes) {
numOldLines := bytes.Count(old, nlBytes)
diffs = append(diffs, protocol.TextEdit{
Range: protocol.Range{
Start: protocol.Position{Line: float64(numOldLines + 1)},
End: protocol.Position{Line: float64(numOldLines + 1)},
},
NewText: "\n",
})
}
return diffs, nil
}

var nlBytes = []byte("\n")

func parseSpan(text string) (start, end int) {
i := strings.Index(text, ",")
if i < 0 {
n, err := strconv.Atoi(text)
if err != nil {
log.Printf("cannot parse span %q", text)
return 0, 0
}
return n, n
}
start, err1 := strconv.Atoi(text[:i])
end, err2 := strconv.Atoi(text[i+1:])
if err1 != nil || err2 != nil {
log.Printf("cannot parse span %q", text)
return 0, 0
}
return start, end
}

func findLines(text []byte, start, end int) []byte {
i := 0

start--
for ; i < len(text) && start > 0; i++ {
if text[i] == '\n' {
start--
end--
}
}
startByte := i
for ; i < len(text) && end > 0; i++ {
if text[i] == '\n' {
end--
}
}
endByte := i
return text[startByte:endByte]
}

func tempfile(body []byte) (string, error) {
f, err := ioutil.TempFile("", "acme-lsp")
if err != nil {
return "", err
}
if _, err := f.Write(body); err != nil {
return "", err
}
tmp := f.Name()
f.Close()
return tmp, nil
}