Skip to content

Commit

Permalink
sync to LuaHelper commit 1379803
Browse files Browse the repository at this point in the history
  • Loading branch information
lalawue committed Sep 15, 2024
1 parent 818edd2 commit a23e50b
Show file tree
Hide file tree
Showing 32 changed files with 1,135 additions and 33 deletions.
5 changes: 4 additions & 1 deletion luahelper-lsp/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
CGO_ENABLED=0 GOARCH=amd64 go build
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build
mv luahelper-lsp ./../luahelper-vscode/server/maclualsp

CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build
mv luahelper-lsp ./../luahelper-vscode/server/armmaclualsp

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
mv luahelper-lsp ./../luahelper-vscode/server/linuxlualsp

Expand Down
62 changes: 62 additions & 0 deletions luahelper-lsp/langserver/check/analysis/analysis_check_enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package analysis

import (
"fmt"
"luahelper-lsp/langserver/check/common"
"luahelper-lsp/langserver/check/compiler/ast"
"luahelper-lsp/langserver/check/compiler/lexer"
)

// 是否给常量赋值 当有a.b.c时,只判断a是否常量 若a是import值,则只判断b是否常量
func (a *Analysis) checkEnum(node *ast.TableConstructorExp) {
if len(node.KeyExps) == 0 {
return
}

if !a.isNeedCheck() || a.realTimeFlag {
return
}

if common.GConfig.IsGlobalIgnoreErrType(common.CheckErrorEnumValue) {
return
}

fileResult := a.curResult

// 判断枚举值是否有关联
if !a.Projects.IsLineFragementEnum(fileResult.Name, node.Loc.StartLine) {
return
}

var tableEnumList common.TableEnumList
for i, keyExp := range node.KeyExps {
valExp := node.ValExps[i]
if valExp == nil {
continue
}

strKeySimple := common.GetExpName(keyExp)
itemNum, flag := tableEnumList.CheckEnumExp(strKeySimple, valExp)
if !flag {
tableEnumList.AddEnumVar(strKeySimple, keyExp, valExp)
continue
}

errStr := fmt.Sprintf("table: enum %s and %s contains duplicate value", itemNum.FieldStr, strKeySimple)
var relateVec []common.RelateCheckInfo
oldKeyLoc := common.GetExpLoc(itemNum.KeyExp)
oldValueLoc := common.GetExpLoc(itemNum.ValueExp)
oldRangeLoc := lexer.GetRangeLoc(&oldKeyLoc, &oldValueLoc)

relateVec = append(relateVec, common.RelateCheckInfo{
LuaFile: fileResult.Name,
ErrStr: errStr,
Loc: oldRangeLoc,
})

expKeyLoc := common.GetExpLoc(keyExp)
expValueLoc := common.GetExpLoc(valExp)
rangeLoc := lexer.GetRangeLoc(&expKeyLoc, &expValueLoc)
fileResult.InsertRelateError(common.CheckErrorEnumValue, errStr, rangeLoc, relateVec)
}
}
2 changes: 2 additions & 0 deletions luahelper-lsp/langserver/check/analysis/analysis_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ func (a *Analysis) cgTableConstructorExp(node *ast.TableConstructorExp, parentVa
tabKeyMap[strKey] = loc
}
}

a.checkEnum(node)
}

// r[a] := op exp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ const (
// ---@field FuncA fun(self:A) : void
FieldColonHide = 2
)

// EnumType 枚举类型start或end
type EnumType uint8

const (
_ EnumType = iota

// EnumTypeNone 非枚举类型
EnumTypeNone = 0

// EnumTypeStart 枚举类型的开始
EnumTypeStart = 1

// EnumTypeEnd 枚举类型的结束
EnumTypeEnd = 2
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package annotateast


// 分析注释功能

// 自带的type类型有下面的
Expand All @@ -20,7 +19,6 @@ package annotateast
// Since lua is dynamic-typed, a variable may be of different types
// use | to list all possible types


// 描述多个的时候
// ---@type MY_TYPE[|OTHER_TYPE], MY_TYPE[|OTHER_TYPE] [@comment] [@comment]

Expand Down Expand Up @@ -67,7 +65,7 @@ function table.concat(list, sep, i, j) end

// 7 @param 函数参数
//---@param param_name MY_TYPE[|other_type] [@comment]
// 7.1 例子
// 7.1 例子
//---@param car Car
//function setCar(car) end

Expand All @@ -86,7 +84,6 @@ function table.concat(list, sep, i, j) end
//---@type (string | number)[] 表示类型为string的列表或是number的列表
//---@type string[] | number[] 表示类型为string的列表或是number的列表


// 9 table type
//---@type table<KEY_TYPE[, KEY_OTHER_TYPE], VALUE_TYPE[, VAULE_OTHER_TYPE]>

Expand All @@ -107,3 +104,6 @@ function table.concat(list, sep, i, j) end
//---@return T
//function test(param1, param2)

// 12 enum 枚举段类型【一个枚举段中定义的变量的值不能重复】
//---@enum start @comment 表示枚举段的开始
//---@enum end @comment 表示枚举段的结束
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ type AnnotateOverloadState struct {

// AnnotateTypeState 定义的类型
// 一行可能定义多个,例如下面的例子
//---@type [const] number, [const] stirng
//local a, b
// ---@type [const] number, [const] stirng
// local a, b
type AnnotateTypeState struct {
ListType []Type // 多个类型,放在list里面
Comment string // 其他所有的注释内容
CommentLoc lexer.Location // 注释内容的位置信息
ListConst []bool // 是否const
ListEnum []bool // 是否为枚举的
}

// AnnotateClassState 定义的class
Expand All @@ -46,7 +47,7 @@ type AnnotateClassState struct {
}

// AnnotateFieldState 定义的成员结构
//---@field [public|protected|private] field_name FIELD_TYPE[|OTHER_TYPE] [@comment]
// ---@field [public|protected|private] field_name FIELD_TYPE[|OTHER_TYPE] [@comment]
type AnnotateFieldState struct {
Name string // 成员结构的名称
NameLoc lexer.Location // field的名称位置
Expand Down Expand Up @@ -78,7 +79,7 @@ type AnnotateReturnState struct {
}

// AnnotateGenericState 泛型的结构
//---@generic T1 [: PARENT_TYPE] [, T2 [: PARENT_TYPE]] @comment @comment
// ---@generic T1 [: PARENT_TYPE] [, T2 [: PARENT_TYPE]] @comment @comment
// todo 泛型还没有处理
type AnnotateGenericState struct {
NameList []string // 可能一行定义多个
Expand All @@ -96,6 +97,21 @@ type AnnotateVarargState struct {
CommentLoc lexer.Location // 注释内容的位置信息
}

// AnnotateEnumState 枚举的结构
type AnnotateEnumState struct {
EnumLoc lexer.Location // enum位置
EnumType EnumType // 枚举的类型
Comment string // 其他所有的注释内容
CommentLoc lexer.Location // 注释内容的位置信息
}

// AnnotateEnumEndState 枚举的结构
type AnnotateEnumEndState struct {
EnumLoc lexer.Location // enum位置
Comment string // 其他所有的注释内容
CommentLoc lexer.Location // 注释内容的位置信息
}

// AnnotateMarkState 分割标记
// -- MARK: any comment
type AnnotateMarkState struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const (
ATokenKwIdentifier // identifier
ATokenKwConst // const
ATokenKwOther // other token, not valid
ATokenKwEnum // enum 枚举段关键值
ATokenKwEnumStart // start enum后面跟着的开始关键字,例如完整的为enum start
ATokenKwEnumEnd // end enum后面跟着的结束关键字,例如完整的为enum end
)

var keywords = map[string]ATokenType{
Expand All @@ -53,4 +56,5 @@ var keywords = map[string]ATokenType{
"private": ATokenKwPrivate,
"vararg": ATokenKwVararg,
"const": ATokenKwConst,
"enum": ATokenKwEnum,
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func parserOneState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState {
return parserGenericState(l)
case annotatelexer.ATokenKwVararg:
return parserVarargState(l)
case annotatelexer.ATokenKwEnum:
return parserEnumState(l)
}

return &annotateast.AnnotateNotValidState{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,37 @@ func parserTypeState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState {
typeState := &annotateast.AnnotateTypeState{}

for {
// const
if l.LookAheadKind() == annotatelexer.ATokenKwConst {
typeState.ListConst = append(typeState.ListConst, true)
l.NextTokenOfKind(annotatelexer.ATokenKwConst)

if l.LookAheadKind() == annotatelexer.ATokenKwEnum {
// const enum
typeState.ListEnum = append(typeState.ListEnum, true)
l.NextTokenOfKind(annotatelexer.ATokenKwEnum)
} else {
// const
typeState.ListEnum = append(typeState.ListEnum, false)
}
} else {
typeState.ListConst = append(typeState.ListConst, false)
// enum
if l.LookAheadKind() == annotatelexer.ATokenKwEnum {
typeState.ListEnum = append(typeState.ListEnum, true)
l.NextTokenOfKind(annotatelexer.ATokenKwEnum)

if l.LookAheadKind() == annotatelexer.ATokenKwConst {
// enum const
typeState.ListConst = append(typeState.ListConst, true)
l.NextTokenOfKind(annotatelexer.ATokenKwConst)
} else {
// enum
typeState.ListConst = append(typeState.ListConst, false)
}
} else {
typeState.ListEnum = append(typeState.ListEnum, false)
typeState.ListConst = append(typeState.ListConst, false)
}
}

oneType := parserOneType(l)
Expand All @@ -41,7 +67,7 @@ func parserTypeState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState {
}

// 解析@Alias
//---@alias NEW_NAME TYPE
// ---@alias NEW_NAME TYPE
func parserAliasState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState {
// skip alias token
l.NextTokenOfKind(annotatelexer.ATokenKwAlias)
Expand Down Expand Up @@ -249,7 +275,7 @@ func parserReturnState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState
}

// 解析@generic
//---@generic T1 [: PARENT_TYPE] [, T2 [: PARENT_TYPE]] @comment @comment
// ---@generic T1 [: PARENT_TYPE] [, T2 [: PARENT_TYPE]] @comment @comment
func parserGenericState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState {
// 前面的关键词为generic 跳过
l.NextTokenOfKind(annotatelexer.ATokenKwGeneric)
Expand Down Expand Up @@ -307,6 +333,39 @@ func parserVarargState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState
return varargState
}

// 解析@enum
// ---@enum start @comment 表示枚举段的开始
// ---@enum end @comment 表示枚举段的结束
func parserEnumState(l *annotatelexer.AnnotateLexer) annotateast.AnnotateState {
nowLoc := l.GetNowLoc()

// 前面的关键词为enum 跳过
l.NextTokenOfKind(annotatelexer.ATokenKwEnum)

enumState := &annotateast.AnnotateEnumState{
EnumLoc: nowLoc,
}

aheadKind := l.LookAheadKind()

if aheadKind == annotatelexer.ATokenKwIdentifier {
// 为其他的标识符
nameStr := l.NextTypeIdentifier()
if nameStr == "start" {
enumState.EnumType = annotateast.EnumTypeStart
} else if nameStr == "end" {
enumState.EnumType = annotateast.EnumTypeEnd
} else {
// 不合法的enum
return &annotateast.AnnotateNotValidState{}
}
}

// 获取这个state的多余注释
enumState.Comment, enumState.CommentLoc = l.GetRemainComment()
return enumState
}

// 解析@mark
// --@mark anything
func parseMarkState(l *annotatelexer.AnnotateLexer) (oneState annotateast.AnnotateState,
Expand Down
Loading

0 comments on commit a23e50b

Please sign in to comment.