mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-19 23:45:27 +02:00
88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
package common
|
|
|
|
import (
|
|
"github.com/antlr4-go/antlr/v4"
|
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
|
)
|
|
|
|
type Location struct {
|
|
Line uint32
|
|
Character uint32
|
|
}
|
|
|
|
type LocationRange struct {
|
|
Start Location
|
|
End Location
|
|
}
|
|
|
|
func (l LocationRange) ToLSPRange() protocol.Range {
|
|
return protocol.Range{
|
|
Start: protocol.Position{
|
|
Line: l.Start.Line,
|
|
Character: l.Start.Character,
|
|
},
|
|
End: protocol.Position{
|
|
Line: l.End.Line,
|
|
Character: l.End.Character,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (l *LocationRange) ChangeBothLines(newLine uint32) {
|
|
l.Start.Line = newLine
|
|
l.End.Line = newLine
|
|
}
|
|
|
|
func (l LocationRange) ContainsCursor(line uint32, character uint32) bool {
|
|
return line == l.Start.Line && character >= l.Start.Character && character <= l.End.Character
|
|
}
|
|
|
|
func (l LocationRange) ContainsCursorByCharacter(character uint32) bool {
|
|
return character >= l.Start.Character && character <= l.End.Character
|
|
}
|
|
|
|
func CreateFullLineRange(line uint32) LocationRange {
|
|
return LocationRange{
|
|
Start: Location{
|
|
Line: line,
|
|
Character: 0,
|
|
},
|
|
End: Location{
|
|
Line: line,
|
|
Character: 999999,
|
|
},
|
|
}
|
|
}
|
|
|
|
func CreateSingleCharRange(line uint32, character uint32) LocationRange {
|
|
return LocationRange{
|
|
Start: Location{
|
|
Line: line,
|
|
Character: character,
|
|
},
|
|
End: Location{
|
|
Line: line,
|
|
Character: character,
|
|
},
|
|
}
|
|
}
|
|
|
|
func CharacterRangeFromCtx(
|
|
ctx antlr.BaseParserRuleContext,
|
|
) LocationRange {
|
|
line := uint32(ctx.GetStart().GetLine())
|
|
start := uint32(ctx.GetStart().GetStart())
|
|
end := uint32(ctx.GetStop().GetStop())
|
|
|
|
return LocationRange{
|
|
Start: Location{
|
|
Line: line,
|
|
Character: start,
|
|
},
|
|
End: Location{
|
|
Line: line,
|
|
Character: end + 1,
|
|
},
|
|
}
|
|
}
|