Autoformatting

This commit is contained in:
Zachary Yedidia
2020-08-12 16:03:14 -04:00
parent 08f772b7d0
commit c1621086a2
7 changed files with 96 additions and 10 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/zyedidia/micro/v2/internal/shell"
"github.com/zyedidia/micro/v2/internal/util"
"github.com/zyedidia/tcell"
"go.lsp.dev/protocol"
)
// ScrollUp is not an action
@@ -1815,6 +1816,9 @@ func (h *BufPane) RemoveAllMultiCursors() bool {
return true
}
// SemanticInfo returns information about the identifier the cursor is on and
// displays the information in the infobar
// The information is fetched using the LSP server (must be enabled)
func (h *BufPane) SemanticInfo() bool {
info, err := h.Buf.Server.Hover(h.Buf.AbsPath, lsp.Position(h.Cursor.X, h.Cursor.Y))
@@ -1829,6 +1833,24 @@ func (h *BufPane) SemanticInfo() bool {
return true
}
// AutoFormat automatically formats the document using LSP
func (h *BufPane) AutoFormat() bool {
edits, err := h.Buf.Server.DocumentFormat(h.Buf.AbsPath, protocol.FormattingOptions{
InsertSpaces: h.Buf.Settings["tabstospaces"].(bool),
TabSize: h.Buf.Settings["tabsize"].(float64),
})
if err != nil {
InfoBar.Error(err)
return false
}
for _, e := range edits {
h.Buf.ApplyEdit(e)
}
return true
}
// None is an action that does nothing
func (h *BufPane) None() bool {
return true

View File

@@ -689,6 +689,7 @@ var BufKeyActions = map[string]BufKeyAction{
"Deselect": (*BufPane).Deselect,
"ClearInfo": (*BufPane).ClearInfo,
"SemanticInfo": (*BufPane).SemanticInfo,
"AutoFormat": (*BufPane).AutoFormat,
"None": (*BufPane).None,
// This was changed to InsertNewline but I don't want to break backwards compatibility