1 Star 0 Fork 0

bughou / go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
position.go 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
bughou 提交于 2022-03-20 18:50 . save
package position
import (
"fmt"
runewidth "github.com/mattn/go-runewidth"
)
func Get(content []rune, offset int) string {
line, column := OffsetToLineAndColumn(content, offset)
if line <= 0 || column <= 0 {
return ""
}
lineDesc := []rune(fmt.Sprintf("Line %d: ", line))
columnDesc := []rune(fmt.Sprintf("Char %d: ", column))
position := lineDesc
position = append(position, GetLineContent(content, offset, column)...)
position = append(position, '\n')
position = append(position, columnDesc...)
if columnEnd := len(lineDesc) + column; columnEnd > len(columnDesc) {
position = append(position, makePadding(position[len(columnDesc):columnEnd-1])...)
position = append(position, '^')
}
return string(position)
}
// line and column begins at 1
func OffsetToLineAndColumn(content []rune, offset int) (int, int) {
if offset < 0 || offset >= len(content) {
return 0, 0
}
var line, column, lastLineWidth int = 1, 0, 0
for i := 0; i <= offset; i++ {
column++
if content[i] == '\n' {
line++
lastLineWidth = column
column = 0
}
}
if column == 0 {
line--
column = lastLineWidth
}
return line, column
}
func GetLineContent(content []rune, offset, column int) []rune {
lineStart := offset - (column - 1)
if lineStart < 0 {
lineStart = 0
}
lineEnd := GetLineEnd(content, offset)
return content[lineStart:lineEnd]
}
func GetLineEnd(content []rune, offset int) int {
if offset < 0 || offset >= len(content) {
return len(content)
}
for i := offset; i < len(content); i++ {
if content[i] == '\n' {
return i
}
}
return len(content)
}
func makePadding(content []rune) (result []rune) {
for _, char := range content {
if char == '\t' {
result = append(result, '\t')
} else {
for i := 0; i < runewidth.RuneWidth(char); i++ {
result = append(result, ' ')
}
}
}
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bughou/go.git
git@gitee.com:bughou/go.git
bughou
go
go
d31700df43a9

搜索帮助

344bd9b3 5694891 D2dac590 5694891