Giter Site home page Giter Site logo

cppcxy / emmyluacodestyle Goto Github PK

View Code? Open in Web Editor NEW
137.0 3.0 25.0 7.78 MB

fast, powerful, and feature-rich Lua formatting and checking tool.

License: MIT License

CMake 0.43% C++ 61.23% C 9.10% Makefile 0.53% HTML 4.39% CSS 0.02% Roff 0.03% Shell 0.38% M4 0.03% Meson 0.01% Lua 22.45% Starlark 0.14% Python 1.24% Batchfile 0.01% SCSS 0.02%
emmylua lua linter lsp formatter

emmyluacodestyle's Introduction

EmmyLuaCodeStyle

English doc

编辑器支持

vscode: vscode-EmmyLuaCodeStyle vscode-Lua

intellij: intellij-EmmyLuaCodeStyle

neovim: Lua Language Server

项目介绍

该项目不是lua代码格式的规范, 而是基于C++实现的lua代码解析\格式化\代码诊断算法库和语言服务实例

该项目主要追求的是合理的格式化,多样的代码风格

该项目除了提供语言服务实例,也提供独立的命令行工具CodeFormat,他能用于批量的代码格式化和代码风格检查。

Feature

  • 支持lua5.1, lua5.2, lua5.3, lua5.4, luajit
  • 文档格式化
  • 范围格式化
  • 命名风格检查
  • 代码格式检查
  • 代码拼写检查
  • 可以作为lua库/cli工具/C++解析lua库/语言服务使用

RoadMap

  • plugin[0%]

文档

Contribute

任何pr或者issue都是欢迎的

Build

如果希望能自己编译该项目,确保自己的编译器能基本支持C++20:

  • VS2019 16.10以上
  • gcc 10以上
  • clang 10以上
mkdir build && cd build
cmake ..
cmake --build . 

Developed By

@CppCXY

Contributors

@obszczymucha

@Rainer Poisel

@AndreaWalchshoferSCCH

@一醉青衫

License

MIT

emmyluacodestyle's People

Contributors

andreawalchshoferscch avatar cppcxy avatar mrz626 avatar notpeter avatar obszczymucha avatar pqn avatar qq792453582 avatar rpoisel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

emmyluacodestyle's Issues

将格式化与风格诊断分离

实践中我们通常希望格式化总是输出出稳定的格式,但是手写代码允许有多种可用的格式,例如:

if 1
or 2 then
elseif 3 then
end
if     1
or     2 then
elseif 3 then
end

格式化的时候我希望总是可以输出成下面的格式,但是我手写时上面的格式也是允许的。
因此希望可以增加诊断专用的 allow 系列设置,这些设置仅仅是用于抑制风格诊断。

  • 格式化时:根据设置进行格式化,不管 allow 系列设置
  • 诊断时:先根据设置进行诊断,然后根据 allow 系列设置排除掉部分诊断

Add option for maximum line separation

As discussed in #33, remove line separation between blocks of code.

For example sake, lets say the variable is set as max_line_seperation = 1 and is the only option that is set.
The formatter would modify the file like so:

Unformatted file:

local M = {}




--- Utilities ---


-- Convert string to title case
function M.firstToUpper(str)
  return (str:gsub('^%l', string.upper))
end


-- Math operations
-- Check if number is prime
function M.is_prime(n)
  local max_divider_checked = math.sqrt(n) + 1
  for i = 2, max_divider_checked do
    if n % i == 0 then
      return false
    end
  end
  return true
end



return M

File diff:

+local M = {}
+
-
-
-
+--- Utilities ---
+
-
+-- Convert string to title case
+function M.firstToUpper(str)
+  return (str:gsub('^%l', string.upper))
+end
+
-
+-- Math operations
+-- Check if number is prime
+function M.is_prime(n)
+  local max_divider_checked = math.sqrt(n) + 1
+  for i = 2, max_divider_checked do
+    if n % i == 0 then
+      return false
+    end
+  end
+  return true
+end
+
-
-
+return M

Final Formatted file:

local M = {}

--- Utilities ---

-- Convert string to title case
function M.firstToUpper(str)
  return (str:gsub('^%l', string.upper))
end

-- Math operations
-- Check if number is prime
function M.is_prime(n)
  local max_divider_checked = math.sqrt(n) + 1
  for i = 2, max_divider_checked do
    if n % i == 0 then
      return false
    end
  end
  return true
end

return M

Default editorconfig

Hi, I use neovim and the sumneko LSP server, and I started using the new code formatting feature. BTW, thanks!!! great job :)
Even though it works great if I have a .editorconfig file in my project root, I wondered if it's possible to set a default configuration, so I don't need to set it in every project, but only when I want to change something.
Thanks!

对于指定作用域内的变量对齐。

-- line base
local function lineunpack(msg, sz)
  local str = skynet.tostring(msg, sz)
  local t = {}
  for s in string.gmatch(str, "[%a%w]+") do
    table.insert(t, s)
  end
  return tunpack(t)
end

比如这个, strt 就不会对齐,不能使用和模块内变量一样的逻辑嘛。

Clear postemptive spaces on comments

before:
grafik
after:
grafik

why doesn't the formatter clear all postemptive spaces? Seems like it ignores the comments

the lua extension has a function that would clear this as well:
grafik

diagnostic task

  • Reimplementing the diagnostic algorithm
  • Use .lualint.json instead of .editorconfig for diagnostic configuration

Wrong indent warnings for neovim

I already posted the issue at lua-language-server repo (please see here), but I am not sure whether I should bring it here as well.

Describe the bug

I am using the formatter and code style check. Here is my config.

nvim_lsp.sumneko_lua.setup {
  cmd = { "lua-language-server", "--preview" },
  settings = {
    Lua = {
      runtime = {
        -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
        version = 'LuaJIT',
        -- Setup your lua path
        path = runtime_path,
      },

      diagnostics = {

        neededFileStatus = {
          ["codestyle-check"] = "Any",
        },

      },

      format = {
        enable = true,
        defaultConfig = {
          indent_style = 'space',
          indent_size = 2,
        },
      },

      workspace = {
        -- Make the server aware of Neovim runtime files
        library = vim.api.nvim_get_runtime_file("", true),
      },
      -- Do not send telemetry data containing a randomized but unique identifier
      telemetry = {
        enable = false,
      },
    },
  },
}

As the config above, my indentation size is 2 spaces. However, it gives me wrong warnings for the indentations in functions.

image

Environment (please complete the following information):

  • OS: macOS (the latest version)
  • Client: neovim

Thank you so much.

对于函数调用的格式化,是否还能再调整一下。

比如此行代码:

  return net.packString("s2c.game.Scene", desc = desc, map  = 1001, npcs = npcs })

格式化为:

  return net.packString("s2c.game.Scene",
                        { desc = desc, map  = 1001, npcs = npcs })

会比当前的:

    return net.packString("s2c.game.Scene",
        { desc = desc, map = 1001, npcs = npcs })

更符合直觉一些。

格式化选定内容/粘贴时格式化的问题

TEST [[
---@class Class

---@param k Class
for <?k?> in pairs(t) do
end
]]
[[
parameter k: Class
]]

选中倒数第二行的 parameter,选择 格式化选定内容 后,会导致倒数第三行格式改变,变为:

TEST [[
---@class Class

---@param k Class
for <?k?> in pairs(t) do
end
]]
    [[
parameter k: Class
]]

Disable formatting temporarily

Thanks a lot for your great work! I really like the formatter and use it frequently. I have a question regarding the functionalities:
Is there a possibility to disable formatting just like the EmmyLua-Annotation diagnostic?
Especially when it comes to automatic line breaks, such a thing would be really useful at certain points.

Code format not running.

I get a 'formatter not installed' error when I try to format code. I have format enabled and I am trying to use the default .editorconfig in my root directory.

当函数调用,以函数定义作为参数时的行为。

问题一:
对于配置了对齐函数调用参数的代码:

skynet.start(function()
               local a = 1
               local b = 2
             end)

实际上这种情况下,配置为不对齐更好看,因为如果函数层级多的话,会出现大量的空白:

skynet.start(function()
  local a = 1
  local b = 2
end)

需求:能否在即时函数定义作为参数的时候,对齐到外层函数调用,而不是对齐到即时函数的定义位置。这种情况下,

  • 如果即时函数作为第一个参数,那应该表现很好。
  • 如果即时函数作为第二、第三个函数,在即时函数前参数和 function 在一行。
  • 如果即时函数后还有参数,那就跟在 end 后面,与它一行。
  • 如果多个即时函数作为参数,那就把后面个即时函数的 function 和 前一个 end 放在一行。
  • 如果参数有表的话又该怎么搞?

问题二:

local sid = skynet.call(".world", "lua", "userScene", { map = 1001, uid = 2 })
local desc, npcs = skynet.call(sid, "lua", "enter", { map = 1001, uid = 2 }) -- return desc

此断代码,不会对齐,诊断报缩进错误。

[enhancement] can we support the following functions?

Hello, Can we add support for the following functions? I use them for C/C++ on vscode. It will be pretty handy for me if they are available in Lua also.

  • SpaceBeforeFunctionOpenParenthesis
  • SpaceBeforeOpenSquareBracket
  • SpaceBeforeLambdaOpenParenthesis
  • SpaceWithinExpression

SpaceBeforeFunctionOpenParenthesis

Before Formatting:

function ff()
end

After Formatting:

function ff ()
end

SpaceBeforeOpenSquareBracket

Before Formatting:

details["names"] = "ashok"

After Formatting:

details ["names"] = "ashok"

SpaceBeforeLambdaOpenParenthesis

Before Formatting:

local details ={}

After Formatting:

local details = {}

SpaceWithinExpression

Before Formatting:

i?"true":"false";
i>0

After Formatting:

i ? "true" : "false";
i > 0

Thank You,
ashok.

多行字符串不对齐,看起来很难受

local sql = fmt("select a, b, c " ..
    "from table " ..
    "where a > 10 and b < 10")

期待的格式:

local sql = fmt("select a, b, c " ..
                "from table " ..
                "where a > 10 and b < 10")

Formatting table with index starting with bracket string results in invalid lua code

The fix (ffd2205) for LuaLS/lua-language-server#1205 doesn't account for indexes with operators.

local correctly_formats = {
    [ [[mystr]] ]       = 123,
    [ -[[mystr]] ]      = 123,
    [ #[[mystr]] ]      = 123,
    [ not [[mystr]] ]   = 123,
    [ ~[[mystr]] ]      = 123,
}
local incorrectly_formats = {
    [ [[mystr]] .. 123 ]    = 123,
    [ [[mystr]] + 123 ]     = 123,
    [ [[mystr]] - 123 ]     = 123,
    [ [[mystr]] * 123 ]     = 123,
    [ [[mystr]] / 123 ]     = 123,
    [ [[mystr]] // 123 ]    = 123,
    [ [[mystr]] % 123 ]     = 123,
    [ [[mystr]] ^ 123 ]     = 123,
    [ [[mystr]] == 123 ]    = 123,
    [ [[mystr]] ~= 123 ]    = 123,
    [ [[mystr]] >  123 ]    = 123,
    [ [[mystr]] <  123 ]    = 123,
    [ [[mystr]] >= 123 ]    = 123,
    [ [[mystr]] <= 123 ]    = 123,
    [ [[mystr]] and 123 ]   = 123,
    [ [[mystr]] or 123 ]    = 123,
    [ [[mystr]] & 123 ]     = 123,
    [ [[mystr]] | 123 ]     = 123,
    [ [[mystr]] ~ 123 ]     = 123,
    [ [[mystr]] >> 123 ]    = 123,
    [ [[mystr]] << 123 ]    = 123,
}

表字段无法对齐

return {
    {
        behaviors = {
            ["ranvier-wander"] = { areaRestricted = true, interval = 30 },
        },
        description = "A furry little squirrel",
        id = "squirrel",
        keywords = { "squirrel" },
        level = 2,
        name = "A Squirrel",
    },
}

这段代码无法对齐字段。

Help removing empty continuous lines?

I'm using sumneko_lua through neovim and testing the new formatting capabilities out using .editorconfig to see if I can't replace stylua as my default formatter but still keep a similar style.

In my config file I have the line max_continuous_line_distance = 1; My assumption is that this would remove empty lines between statements and would format the examples below, but it doesn't appear to do anything...

How can I achieve this result?
It successfully fixes indentation and providing diagnostics so the rest of the config is clearly working.

print('hello, world')


--- Modules ---




require('lsp.handlers')
require('lsp.null-ls')

require('lsp.installer').setup({


  capabilities = capabilities,
        language_servers = language_servers,

  on_attach = on_attach,
})


local example1 = 'foo'
local example2 = 'bar'
print('hello, world')

--- Modules ---

require('lsp.handlers')
require('lsp.null-ls')
require('lsp.installer').setup({
  capabilities = capabilities,
  language_servers = language_servers,
  on_attach = on_attach,
})

local example1 = 'foo'
local example2 = 'bar'

align_call_args = false should keep the indent

local function describe(tag, f) end

describe('long' ..
    'tag', function()
end)

After formating:

  • align_call_args = true
local function describe(tag, f) end

describe('long' ..
    'tag', function()
    end)

end) have been indented.

  • align_call_args = false
local function describe(tag, f) end

describe('long' ..
'tag', function()
end)

tag has been deindented.

Expected behavior:

Keep the raw format if align_call_args = false .

Binary in Release zip can't be executed

unzip -Z linux-x64.zip output:

Archive:  linux-x64.zip
Zip file size: 2812943 bytes, number of entries: 4
drwxr-xr-x  3.0 unx        0 bx stor 22-Apr-12 17:23 linux-x64/
drwxr-xr-x  3.0 unx        0 bx stor 22-Apr-12 17:23 linux-x64/bin/
-rw-r--r--  3.0 unx  6992816 bx defN 22-Apr-12 17:23 linux-x64/bin/CodeFormatServer
-rw-r--r--  3.0 unx  3900568 bx defN 22-Apr-12 17:23 linux-x64/bin/CodeFormat
4 files, 10893384 bytes uncompressed, 2812253 bytes compressed:  74.2%

I want to use this tool in GitHub action. tar.gz format is better.

指定字符串使用单引号还是双引号

在指定使用单引号时:

print('xxx', "xxx")

格式化为

print('xxx', 'xxx')

例外规则:字符串内包含指定的引号类型时不进行格式化,例如:

print('xxx', "xxx", "xx'xx")

格式化为

print('xxx', 'xxx', "xx'xx")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.