Skip to content

Commit

Permalink
代码完善
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed May 13, 2024
1 parent 6890a28 commit 6000657
Show file tree
Hide file tree
Showing 18 changed files with 2,190 additions and 46 deletions.
183 changes: 137 additions & 46 deletions internal/lsp/protocol/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// this file contains protocol<->span converters

package protocol

import (
"fmt"
"unicode/utf8"

"wa-lang.org/wa/internal/lsp/span"
)

// CompareLocation defines a three-valued comparison over locations,
Expand All @@ -22,52 +26,6 @@ func CompareLocation(x, y Location) int {
return CompareRange(x.Range, y.Range)
}

// CompareRange returns -1 if a is before b, 0 if a == b, and 1 if a is after b.
//
// A range a is defined to be 'before' b if a.Start is before b.Start, or
// a.Start == b.Start and a.End is before b.End.
func CompareRange(a, b Range) int {
if r := ComparePosition(a.Start, b.Start); r != 0 {
return r
}
return ComparePosition(a.End, b.End)
}

// ComparePosition returns -1 if a is before b, 0 if a == b, and 1 if a is after b.
func ComparePosition(a, b Position) int {
if a.Line != b.Line {
if a.Line < b.Line {
return -1
} else {
return +1
}
}
if a.Character != b.Character {
if a.Character < b.Character {
return -1
} else {
return +1
}
}
return 0
}

func Intersect(a, b Range) bool {
if a.Start.Line > b.End.Line || a.End.Line < b.Start.Line {
return false
}
return !((a.Start.Line == b.End.Line) && a.Start.Character > b.End.Character ||
(a.End.Line == b.Start.Line) && a.End.Character < b.Start.Character)
}

// Format implements fmt.Formatter.
//
// Note: Formatter is implemented instead of Stringer (presumably) for
// performance reasons, though it is not clear that it matters in practice.
func (r Range) Format(f fmt.State, _ rune) {
fmt.Fprintf(f, "%v-%v", r.Start, r.End)
}

// Format implements fmt.Formatter.
//
// See Range.Format for discussion of why the Formatter interface is
Expand Down Expand Up @@ -98,3 +56,136 @@ func UTF16Len(s []byte) int {
}
return n
}

type ColumnMapper struct {
URI span.URI
Converter *span.TokenConverter
Content []byte
}

func URIFromSpanURI(uri span.URI) DocumentURI {
return DocumentURI(uri)
}

func (u DocumentURI) SpanURI() span.URI {
return span.URIFromURI(string(u))
}

func (m *ColumnMapper) Location(s span.Span) (Location, error) {
rng, err := m.Range(s)
if err != nil {
return Location{}, err
}
return Location{URI: URIFromSpanURI(s.URI()), Range: rng}, nil
}

func (m *ColumnMapper) Range(s span.Span) (Range, error) {
if span.CompareURI(m.URI, s.URI()) != 0 {
return Range{}, fmt.Errorf("column mapper is for file %q instead of %q", m.URI, s.URI())
}
s, err := s.WithAll(m.Converter)
if err != nil {
return Range{}, err
}
start, err := m.Position(s.Start())
if err != nil {
return Range{}, err
}
end, err := m.Position(s.End())
if err != nil {
return Range{}, err
}
return Range{Start: start, End: end}, nil
}

func (m *ColumnMapper) Position(p span.Point) (Position, error) {
chr, err := span.ToUTF16Column(p, m.Content)
if err != nil {
return Position{}, err
}
return Position{
Line: uint32(p.Line() - 1),
Character: uint32(chr - 1),
}, nil
}

func (m *ColumnMapper) Span(l Location) (span.Span, error) {
return m.RangeSpan(l.Range)
}

func (m *ColumnMapper) RangeSpan(r Range) (span.Span, error) {
start, err := m.Point(r.Start)
if err != nil {
return span.Span{}, err
}
end, err := m.Point(r.End)
if err != nil {
return span.Span{}, err
}
return span.New(m.URI, start, end).WithAll(m.Converter)
}

func (m *ColumnMapper) RangeToSpanRange(r Range) (span.Range, error) {
spn, err := m.RangeSpan(r)
if err != nil {
return span.Range{}, err
}
return spn.Range(m.Converter)
}

func (m *ColumnMapper) PointSpan(p Position) (span.Span, error) {
start, err := m.Point(p)
if err != nil {
return span.Span{}, err
}
return span.New(m.URI, start, start).WithAll(m.Converter)
}

func (m *ColumnMapper) Point(p Position) (span.Point, error) {
line := int(p.Line) + 1
offset, err := m.Converter.ToOffset(line, 1)
if err != nil {
return span.Point{}, err
}
lineStart := span.NewPoint(line, 1, offset)
return span.FromUTF16Column(lineStart, int(p.Character)+1, m.Content)
}

func IsPoint(r Range) bool {
return r.Start.Line == r.End.Line && r.Start.Character == r.End.Character
}

func CompareRange(a, b Range) int {
if r := ComparePosition(a.Start, b.Start); r != 0 {
return r
}
return ComparePosition(a.End, b.End)
}

func ComparePosition(a, b Position) int {
if a.Line < b.Line {
return -1
}
if a.Line > b.Line {
return 1
}
if a.Character < b.Character {
return -1
}
if a.Character > b.Character {
return 1
}
return 0
}

func Intersect(a, b Range) bool {
if a.Start.Line > b.End.Line || a.End.Line < b.Start.Line {
return false
}
return !((a.Start.Line == b.End.Line) && a.Start.Character > b.End.Character ||
(a.End.Line == b.Start.Line) && a.End.Character < b.Start.Character)
}

func (r Range) Format(f fmt.State, _ rune) {
fmt.Fprintf(f, "%v:%v-%v:%v", r.Start.Line, r.Start.Character, r.End.Line, r.End.Character)
}
30 changes: 30 additions & 0 deletions internal/lsp/source/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 版权 @2024 凹语言 作者。保留所有权利。

package source

import "wa-lang.org/wa/internal/lsp/protocol"

// Analyzer represents a go/analysis analyzer with some boolean properties
// that let the user know how to use the analyzer.
type Analyzer struct {
// Analyzer *analysis.Analyzer

// Enabled reports whether the analyzer is enabled. This value can be
// configured per-analysis in user settings. For staticcheck analyzers,
// the value of the Staticcheck setting overrides this field.
Enabled bool

// Fix is the name of the suggested fix name used to invoke the suggested
// fixes for the analyzer. It is non-empty if we expect this analyzer to
// provide its fix separately from its diagnostics. That is, we should apply
// the analyzer's suggested fixes through a Command, not a TextEdit.
Fix string

// ActionKind is the kind of code action this analyzer produces. If
// unspecified the type defaults to quickfix.
ActionKind []protocol.CodeActionKind

// Severity is the severity set for diagnostics reported by this
// analyzer. If left unset it defaults to Warning.
Severity protocol.DiagnosticSeverity
}
45 changes: 45 additions & 0 deletions internal/lsp/source/diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 版权 @2024 凹语言 作者。保留所有权利。

package source

import (
"wa-lang.org/wa/internal/lsp/protocol"
"wa-lang.org/wa/internal/lsp/span"
)

type SuggestedFix struct {
Title string
Edits map[span.URI][]protocol.TextEdit
Command *protocol.Command
ActionKind protocol.CodeActionKind
}

type RelatedInformation struct {
URI span.URI
Range protocol.Range
Message string
}

// An Diagnostic corresponds to an LSP Diagnostic.
// https://microsoft.github.io/language-server-protocol/specification#diagnostic
type Diagnostic struct {
URI span.URI
Range protocol.Range
Severity protocol.DiagnosticSeverity
Code string
CodeHref string

// Source is a human-readable description of the source of the error.
// Diagnostics generated by an analysis.Analyzer set it to Analyzer.Name.
Source string

Message string

Tags []protocol.DiagnosticTag
Related []RelatedInformation

// Fields below are used internally to generate quick fixes. They aren't
// part of the LSP spec and don't leave the server.
SuggestedFixes []SuggestedFix
Analyzer *Analyzer
}

0 comments on commit 6000657

Please sign in to comment.