Skip to content

Conversation

CppCXY
Copy link
Member

@CppCXY CppCXY commented Apr 27, 2023

这个PR是重新引入命名风格检查. 但不再是通过editorconfig设置命名风格.

概述

命名风格检查支持基于基本命名法的检查:

  • snake-case
  • camel-case
  • pascal-case
  • upper-snake-case
    同时也支持指定名称和正则表达式. 以及正则表达式和基本命名法的结合检查方式.

配置

开启命名风格检查需要添加配置:

  "Lua.diagnostics.neededFileStatus": {
      "name-style-check": "Any"
  }

在vscode的setting中增加配置项Lua.nameStyle.config, 可如下填写:

  "Lua.nameStyle.config": {
      "local_name_style": "snake_case"
  },

可配置的项有:

  • local_name_style
  • function_param_name_style
  • function_name_style
  • local_function_name_style
  • table_field_name_style
  • global_variable_name_style
  • module_name_style
  • require_module_name_style
  • class_name_style

每一个可配置项的格式都是相同的, 每个可配置项可配置的值支持如下格式:

  • 单字符串 例如:
"local_name_style": "snake_case"
  • 字符串数组 例如:
"local_name_style": [
    "snake_case", "upper_snake_case"
]
  • 表结构

表结构的一般形式是:

{
    "type": "same", 
    "param": "m"
}

正则表达式的形式是:

{
    "type" : "pattern",
    "param": "uuu*"
}

正则语法为javascript正则语法.
正则表达式支持捕获组后再匹配基本命名法:

{
    "type" : "pattern",
    "param": "m_(w+)",
    "$1": "camel_case"
}
  • 支持表结构和字符串的混合数组 例如:
"local_name_style": [
    "snake_case",
    "pascal_case",
    "camel_case",
    {
        "type" : "pattern",
        "param": "m_(w+)",
        "$1": "camel_case"
    }
]

效果

image

@actboy168
Copy link
Collaborator

actboy168 commented Apr 27, 2023

这个已经超过了format应该做的事?

现在已经有一些针对命名的检查,应在此基础上扩展,而不是保留两套代码。

@CppCXY
Copy link
Member Author

CppCXY commented Apr 27, 2023

这个已经超过了format应该做的事?

EmmyLuaCodeStyle 并没有说自己只是formatter.
这个库本身就已经给LuaLs引入了代码风格检查和拼写检查, 而且命名风格检查本来就是以前提供的, 现在也只是恢复这部分功能.

@AndreaWalchshoferSCCH
Copy link
Contributor

AndreaWalchshoferSCCH commented Apr 28, 2023

As a user I would expect the namestyle-functionality of EmmyLuaCodeStyle to work in LuaLS as soon as you look through the docs of the codestyle library.

I've tried the code of this PR with the latest EmmyLuaCodeStyle code in the submodule and it worked just as I was expecting it to do before this PR. The only thing I'm still missing here is that I can configure the namestyle from both settings.json and .luarc.json or similar, to have the configuration of the diagnostic at the same place as where I enable the diagnostic itself.

It works when using the LuaLS in CLI, so I want to redact my previous comment. Thanks for the implementation!

@sumneko
Copy link
Collaborator

sumneko commented May 9, 2023

我漏了这个PR,你合一下最新版本看看自动测试过了没

@sumneko sumneko merged commit b422860 into LuaLS:master May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants