refactor(lsp): use format package for document formatting
Assisted-by: MiMo V2.5 Pro
This commit is contained in:
+2
-87
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"sourcedock.dev/petrbalvin/gasm-devkit/arch"
|
"sourcedock.dev/petrbalvin/gasm-devkit/arch"
|
||||||
"sourcedock.dev/petrbalvin/gasm-devkit/ast"
|
"sourcedock.dev/petrbalvin/gasm-devkit/ast"
|
||||||
|
"sourcedock.dev/petrbalvin/gasm-devkit/format"
|
||||||
"sourcedock.dev/petrbalvin/gasm-devkit/lexer"
|
"sourcedock.dev/petrbalvin/gasm-devkit/lexer"
|
||||||
"sourcedock.dev/petrbalvin/gasm-devkit/parser"
|
"sourcedock.dev/petrbalvin/gasm-devkit/parser"
|
||||||
"sourcedock.dev/petrbalvin/gasm-devkit/token"
|
"sourcedock.dev/petrbalvin/gasm-devkit/token"
|
||||||
@@ -211,11 +212,7 @@ func (s *Server) documentFormatting(p documentFormattingParams) []TextEdit {
|
|||||||
if text == "" {
|
if text == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
f, _ := parser.Parse(uriPath(p.TextDocument.URI), text)
|
formatted := format.Source(uriPath(p.TextDocument.URI), text)
|
||||||
if f == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
formatted := formatFile(f)
|
|
||||||
if formatted == text {
|
if formatted == text {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -260,88 +257,6 @@ func (s *Server) inlayHints(p inlayHintParams) []InlayHint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// formatFile produces a canonical formatting of a parsed file.
|
// formatFile produces a canonical formatting of a parsed file.
|
||||||
func formatFile(f *ast.File) string {
|
|
||||||
var b strings.Builder
|
|
||||||
for i, d := range f.Decls {
|
|
||||||
if i > 0 {
|
|
||||||
b.WriteByte('\n')
|
|
||||||
}
|
|
||||||
switch dd := d.(type) {
|
|
||||||
case *ast.Include:
|
|
||||||
fmt.Fprintf(&b, "#include %s\n", dd.Header.Text)
|
|
||||||
case *ast.Preproc:
|
|
||||||
b.WriteString("#")
|
|
||||||
b.WriteString(dd.Raw)
|
|
||||||
b.WriteByte('\n')
|
|
||||||
case *ast.Text:
|
|
||||||
fmt.Fprintf(&b, "TEXT %s(SB), %s, $%d-%d\n",
|
|
||||||
symString(dd.Name), strings.Join(dd.Flags, "|"), frameVal(dd.Frame), argsVal(dd.Args))
|
|
||||||
for _, stmt := range dd.Body {
|
|
||||||
switch st := stmt.(type) {
|
|
||||||
case *ast.Label:
|
|
||||||
fmt.Fprintf(&b, "%s:\n", st.Name.Text)
|
|
||||||
case *ast.Instr:
|
|
||||||
b.WriteByte('\t')
|
|
||||||
b.WriteString(st.Mnemonic.Text)
|
|
||||||
for j, op := range st.Operands {
|
|
||||||
if j > 0 {
|
|
||||||
b.WriteString(", ")
|
|
||||||
}
|
|
||||||
b.WriteString(op.Raw)
|
|
||||||
}
|
|
||||||
if st.Comment != "" {
|
|
||||||
b.WriteString(st.Comment)
|
|
||||||
}
|
|
||||||
b.WriteByte('\n')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *ast.Globl:
|
|
||||||
fmt.Fprintf(&b, "GLOBL %s(SB), %s, $%d\n",
|
|
||||||
symString(dd.Name), strings.Join(dd.Flags, "|"), globlSize(dd.Size))
|
|
||||||
case *ast.Data:
|
|
||||||
fmt.Fprintf(&b, "DATA %s+%d(SB)/%d, %s\n",
|
|
||||||
symString(dd.Name), dd.Name.Offset, dd.Width, dd.Value.Raw)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func symString(sym *ast.Symbol) string {
|
|
||||||
if sym == nil {
|
|
||||||
return "<?>"
|
|
||||||
}
|
|
||||||
s := ""
|
|
||||||
if sym.Pkg != "" {
|
|
||||||
s = sym.Pkg + "·"
|
|
||||||
}
|
|
||||||
s += sym.Name
|
|
||||||
if sym.Static {
|
|
||||||
s += "<>"
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func frameVal(op *ast.Operand) int64 {
|
|
||||||
if op != nil && op.Imm.HasVal {
|
|
||||||
return op.Imm.Val
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func argsVal(op *ast.Operand) int64 {
|
|
||||||
if op != nil && op.Imm.HasVal {
|
|
||||||
return op.Imm.Val
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func globlSize(op *ast.Operand) int64 {
|
|
||||||
if op != nil && op.Imm.HasVal {
|
|
||||||
return op.Imm.Val
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// documentSymbols returns functions and their labels, plus global symbols.
|
// documentSymbols returns functions and their labels, plus global symbols.
|
||||||
func (s *Server) documentSymbols(p documentSymbolParams) []DocumentSymbol {
|
func (s *Server) documentSymbols(p documentSymbolParams) []DocumentSymbol {
|
||||||
text := s.docs[p.TextDocument.URI]
|
text := s.docs[p.TextDocument.URI]
|
||||||
|
|||||||
Reference in New Issue
Block a user