|
| 1 | +// Copyright 2020-2025 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// This file provides helpers for bridging protocompile and LSP diagnostics. |
| 16 | + |
| 17 | +package buflsp |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/bufbuild/protocompile/experimental/report" |
| 24 | + "go.lsp.dev/protocol" |
| 25 | +) |
| 26 | + |
| 27 | +// UTF-16 is the default per LSP spec. Position encoding negotiation is not yet |
| 28 | +// supported by the go.lsp.dev/protocol library. |
| 29 | +// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position |
| 30 | +const positionalEncoding = report.UTF16Length |
| 31 | + |
| 32 | +// diagnosticData is a structure to hold the [report.Diagnostic] notes, help, and debug |
| 33 | +// messages, to marshal into JSON for the [protocol.Diagnostic].Data field. |
| 34 | +type diagnosticData struct { |
| 35 | + Notes string `json:"notes,omitempty"` |
| 36 | + Help string `json:"help,omitempty"` |
| 37 | + Debug string `json:"debug,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +// reportLevelToDiagnosticSeverity is a mapping of [report.Level] to [protocol.DiagnosticSeverity]. |
| 41 | +var reportLevelToDiagnosticSeverity = map[report.Level]protocol.DiagnosticSeverity{ |
| 42 | + report.ICE: protocol.DiagnosticSeverityError, |
| 43 | + report.Error: protocol.DiagnosticSeverityError, |
| 44 | + report.Warning: protocol.DiagnosticSeverityWarning, |
| 45 | + report.Remark: protocol.DiagnosticSeverityInformation, |
| 46 | +} |
| 47 | + |
| 48 | +// reportDiagnosticToProtocolDiagnostic takes a [report.Diagnostic] and returns the |
| 49 | +// corresponding [protocol.Diagnostic]. |
| 50 | +func reportDiagnosticToProtocolDiagnostic( |
| 51 | + reportDiagnostic report.Diagnostic, |
| 52 | +) (protocol.Diagnostic, error) { |
| 53 | + diagnostic := protocol.Diagnostic{ |
| 54 | + Source: serverName, |
| 55 | + Severity: reportLevelToDiagnosticSeverity[reportDiagnostic.Level()], |
| 56 | + Message: reportDiagnostic.Message(), |
| 57 | + } |
| 58 | + if primary := reportDiagnostic.Primary(); !primary.IsZero() { |
| 59 | + startLocation := primary.Location(primary.Start, positionalEncoding) |
| 60 | + endLocation := primary.Location(primary.End, positionalEncoding) |
| 61 | + diagnostic.Range = protocol.Range{ |
| 62 | + Start: protocol.Position{ |
| 63 | + Line: uint32(startLocation.Line - 1), |
| 64 | + Character: uint32(startLocation.Column - 1), |
| 65 | + }, |
| 66 | + End: protocol.Position{ |
| 67 | + Line: uint32(endLocation.Line - 1), |
| 68 | + Character: uint32(endLocation.Column - 1), |
| 69 | + }, |
| 70 | + } |
| 71 | + } |
| 72 | + data := diagnosticData{ |
| 73 | + Notes: strings.Join(reportDiagnostic.Notes(), "\n"), |
| 74 | + Help: strings.Join(reportDiagnostic.Help(), "\n"), |
| 75 | + Debug: strings.Join(reportDiagnostic.Debug(), "\n"), |
| 76 | + } |
| 77 | + bytes, err := json.Marshal(data) |
| 78 | + if err != nil { |
| 79 | + return protocol.Diagnostic{}, err |
| 80 | + } |
| 81 | + if bytes != nil { |
| 82 | + // We serialize the bytes into a string before providing the structure to diagnostic.Data |
| 83 | + // because diagnostic.Data is an interface{}, which is treated as a JSON "any", which |
| 84 | + // will not cleanly deserialize. |
| 85 | + diagnostic.Data = string(bytes) |
| 86 | + } |
| 87 | + return diagnostic, nil |
| 88 | +} |
0 commit comments