From 8190738e3deacfce409a5a31df13d772eb04d527 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Fri, 16 Nov 2018 12:16:36 -0500 Subject: [PATCH] internal/lsp: add formatting functions to some common lsp protocol types These are intended only for debug printing and error messages for the most common protocol types that you need to know the contents of. Change-Id: I1f0f7b17b4b12c3b26096b46808bce37c3722ced Reviewed-on: https://go-review.googlesource.com/c/150037 Reviewed-by: Rebecca Stambler --- internal/lsp/protocol/printers.go | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/lsp/protocol/printers.go diff --git a/internal/lsp/protocol/printers.go b/internal/lsp/protocol/printers.go new file mode 100644 index 0000000000..e658af96b0 --- /dev/null +++ b/internal/lsp/protocol/printers.go @@ -0,0 +1,39 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains formatting functions for types that +// are commonly printed in debugging information. +// They are separated from their types and gathered here as +// they are hand written and not generated from the spec. +// They should not be relied on for programmatic use (their +// results should never be parsed for instance) but are meant +// for temporary debugging and error messages. + +package protocol + +import ( + "fmt" +) + +func (p Position) Format(f fmt.State, c rune) { + fmt.Fprintf(f, "%d", int(p.Line)) + if p.Character >= 0 { + fmt.Fprintf(f, ":%d", int(p.Character)) + } +} + +func (r Range) Format(f fmt.State, c rune) { + switch { + case r.Start == r.End || r.End.Line < 0: + fmt.Fprintf(f, "%v", r.Start) + case r.End.Line == r.Start.Line: + fmt.Fprintf(f, "%v¦%d", r.Start, int(r.End.Character)) + default: + fmt.Fprintf(f, "%v¦%v", r.Start, r.End) + } +} + +func (l Location) Format(f fmt.State, c rune) { + fmt.Fprintf(f, "%s:%v", l.URI, l.Range) +}