Skip to content

Commit

Permalink
add RangeFormatting
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihaopan committed Jul 4, 2024
1 parent 92217e6 commit 742d8a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions components/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,37 @@ func Format(ctx context.Context, req *defines.DocumentFormattingParams) (result
},
}, nil
}

func FormatRange(ctx context.Context, req *defines.DocumentRangeFormattingParams) (result *[]defines.TextEdit, err error) {
if !view.IsProtoFile(req.TextDocument.Uri) {
return nil, nil
}
format := exec.Command("clang-format", fmt.Sprintf("--assume-filename=%v", filepath.Base(string(req.TextDocument.Uri))), fmt.Sprintf("--lines=%v:%v", req.Range.Start.Line+1, req.Range.End.Line+1))
in, err := format.StdinPipe()
if err != nil {
return nil, err
}
proto_file, err := view.ViewManager.GetFile(req.TextDocument.Uri)
if err != nil {
return nil, err
}
data, _, _ := proto_file.Read(ctx)
go func() {
io.WriteString(in, string(data))
defer in.Close()
}()

res, err := format.CombinedOutput()
if err != nil {
log.Fatal(err)
}
return &[]defines.TextEdit{
{
Range: defines.Range{
Start: defines.Position{Line: 0, Character: 0},
End: defines.Position{Line: math.MaxInt32, Character: math.MaxInt32},
},
NewText: string(res),
},
}, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ func main() {
server.OnDocumentFormatting(components.Format)
server.OnCompletion(components.Completion)
server.OnHover(components.Hover)
server.OnDocumentRangeFormatting(components.FormatRange)
server.Run()
}

0 comments on commit 742d8a1

Please sign in to comment.