├── .editorconfig ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── cmake └── modules │ └── FindEditorConfig.cmake ├── editorconfig-core-scm-1.rockspec ├── editorconfig_lua.c ├── main.lua └── test.lua.in /.editorconfig: -------------------------------------------------------------------------------- 1 | 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = LF 7 | 8 | [*.[ch]] 9 | indent_style = space 10 | indent_size = 4 11 | 12 | [CMakeLists.txt] 13 | indent_style = space 14 | indent_size = 4 15 | 16 | [*.cmake] 17 | indent_style = space 18 | indent_size = 4 19 | 20 | [*.lua.in] 21 | indent_style = space 22 | indent_size = 4 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.rock 4 | 5 | build/ 6 | build.luarocks/ 7 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tests"] 2 | path = tests 3 | url = git://github.com/editorconfig/editorconfig-core-test.git 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2016 João Valverde 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright notice, 9 | # this list of conditions and the following disclaimer. 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, 11 | # this list of conditions and the following disclaimer in the documentation 12 | # and/or other materials provided with the distribution. 13 | # 14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | # POSSIBILITY OF SUCH DAMAGE. 25 | # 26 | 27 | cmake_minimum_required(VERSION 3.0) 28 | cmake_policy(SET CMP0074 NEW) 29 | 30 | project(editorconfig-core-lua VERSION 0.3.1 LANGUAGES C) 31 | 32 | set(ECL_VERSION "${PROJECT_VERSION}-git") 33 | 34 | # 35 | # EditorConfig Core Lua is a very thin shim around the EditorConfig C 36 | # Library. The vast majority if not all test failures will be because 37 | # the C library version being used is failing the test as well. 38 | # 39 | option(ENABLE_TESTS "Enable running the test suite" OFF) 40 | 41 | set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules") 42 | 43 | include(GNUInstallDirs) 44 | 45 | find_package(EditorConfig REQUIRED) 46 | set(Lua_FIND_VERSION 5.2) # minimum Lua version 47 | find_package(Lua REQUIRED) 48 | 49 | set(CMAKE_C_STANDARD 99) 50 | set(CMAKE_C_STANDARD_REQUIRE ON) 51 | 52 | add_library(editorconfig_lua MODULE editorconfig_lua.c) 53 | target_compile_definitions(editorconfig_lua 54 | PRIVATE "ECL_VERSION=\"${ECL_VERSION}\"" 55 | ) 56 | if(CMAKE_COMPILER_IS_GNUCC) 57 | target_compile_options(editorconfig_lua 58 | PUBLIC -Wall -Wextra 59 | ) 60 | endif() 61 | target_include_directories(editorconfig_lua SYSTEM 62 | PRIVATE ${EDITORCONFIG_INCLUDE_DIRS} ${LUA_INCLUDE_DIR} 63 | ) 64 | target_link_libraries(editorconfig_lua 65 | ${EDITORCONFIG_LIBRARIES} 66 | ${LUA_LIBRARIES} 67 | ) 68 | set_target_properties(editorconfig_lua PROPERTIES 69 | # Omit the lib.so prefix from the DSO 70 | PREFIX "" 71 | OUTPUT_NAME "editorconfig" 72 | ) 73 | 74 | set(ECL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/lua/${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}" 75 | CACHE PATH "Target installation directory." 76 | ) 77 | 78 | install(TARGETS editorconfig_lua 79 | DESTINATION ${ECL_LIBDIR} 80 | ) 81 | 82 | find_program(LDOC_CMD ldoc) 83 | add_custom_target(doc 84 | COMMAND ${LDOC_CMD} ${CMAKE_SOURCE_DIR}/editorconfig_lua.c 85 | ) 86 | 87 | # Testing. Type "make test" to run tests. Only if the test submodule is 88 | # checked out should we do this. 89 | if(ENABLE_TESTS) 90 | if(EXISTS ${CMAKE_SOURCE_DIR}/tests/CMakeLists.txt) 91 | enable_testing() 92 | find_program(LUA_CMD lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}) 93 | configure_file(test.lua.in test.lua @ONLY) 94 | set(EDITORCONFIG_CMD ${LUA_CMD} ${CMAKE_BINARY_DIR}/test.lua) 95 | add_subdirectory(tests) 96 | else() 97 | message(WARNING 98 | "Testing files not found. Testing will not be available. If you obtained the source tree through git, please run 'git submodule update --init' to update the tests submodule.") 99 | endif() 100 | endif() 101 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2016 João Valverde 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EditorConfig Lua Core 2 | 3 | EditorConfig Lua Core provides the same functionality as the 4 | [EditorConfig C Core][1] library. 5 | 6 | ## EditorConfig Project 7 | 8 | EditorConfig makes it easy to maintain the correct coding style when switching 9 | between different text editors and between different projects. The EditorConfig 10 | project maintains a file format and plugins for various text editors which 11 | allow this file format to be read and used by those editors. For information 12 | on the file format and supported text editors, see the [EditorConfig][2] 13 | website. 14 | 15 | ## Installation 16 | 17 | #### Build using CMake 18 | 19 | ``` 20 | mkdir build 21 | cd build 22 | cmake .. 23 | make 24 | sudo make install 25 | ``` 26 | 27 | Or just copy the `editorconfig.so` binary module to somewhere in 28 | your `LUA_CPATH`. 29 | 30 | #### Install using LuaRocks 31 | 32 | You can also install the latest release using [LuaRocks][3]: 33 | 34 | ``` 35 | luarocks [--local] install editorconfig-core 36 | ``` 37 | 38 | ## Usage 39 | 40 | The `parse` module function returns a name/value property table. Typical usage 41 | by plugins would be: 42 | 43 | ```lua 44 | ec = require("editorconfig") 45 | 46 | for name, value in pairs(ec.parse("/full/path/to/file")) do 47 | configure_property[name](value) 48 | end 49 | ``` 50 | 51 | Sometimes it is useful to have the same stable order for each `parse` 52 | invocation that the EditorConfig C Core library provides. For that the property 53 | names are available as an array in a second return value: 54 | 55 | ```lua 56 | prop, names = ec.parse("/full/path/to/file") 57 | print(#names .. " properties:") 58 | for idx, name in ipairs(names) do 59 | print(string.format("%s: %s=%s", idx, name, prop[name])) 60 | end 61 | ``` 62 | 63 | Note also the use of the length operator to retrieve the EditorConfig 64 | property count for a given file. 65 | 66 | #### Note on API stability 67 | 68 | Version 0.3.0 introduced major backward incompatibilities. 69 | 70 | * The `open`function was removed. 71 | * Every EditorConfig value has the Lua type `string`. 72 | * Lua module renamed to `editorconfig`. 73 | 74 | Please update accordingly. 75 | 76 | [1]: https://github.com/editorconfig/editorconfig-core-c 77 | [2]: https://editorconfig.org 78 | [3]: https://luarocks.org 79 | -------------------------------------------------------------------------------- /cmake/modules/FindEditorConfig.cmake: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2016 João Valverde 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are met: 7 | # 8 | # 1. Redistributions of source code must retain the above copyright notice, 9 | # this list of conditions and the following disclaimer. 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, 11 | # this list of conditions and the following disclaimer in the documentation 12 | # and/or other materials provided with the distribution. 13 | # 14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | # POSSIBILITY OF SUCH DAMAGE. 25 | # 26 | 27 | find_path(EDITORCONFIG_INCLUDE_DIR editorconfig/editorconfig.h) 28 | 29 | find_library(EDITORCONFIG_LIBRARY editorconfig) 30 | 31 | include(FindPackageHandleStandardArgs) 32 | 33 | # handle the QUIETLY and REQUIRED arguments and set EDITORCONFIG_FOUND to TRUE if 34 | # all listed variables are TRUE 35 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(EditorConfig DEFAULT_MSG 36 | EDITORCONFIG_LIBRARY EDITORCONFIG_INCLUDE_DIR) 37 | 38 | mark_as_advanced(EDITORCONFIG_INCLUDE_DIR EDITORCONFIG_LIBRARY) 39 | 40 | set(EDITORCONFIG_LIBRARIES ${EDITORCONFIG_LIBRARY}) 41 | set(EDITORCONFIG_INCLUDE_DIRS ${EDITORCONFIG_INCLUDE_DIR}) 42 | -------------------------------------------------------------------------------- /editorconfig-core-scm-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "editorconfig-core" 2 | -- local _version = @VERSION@ 3 | local _version = "scm" 4 | local _pkgrel = "1" 5 | version = _version .. "-" .. _pkgrel 6 | source = { 7 | -- url = "https://github.com/editorconfig/editorconfig-core-lua/archive/v" .. _version .. ".tar.gz", 8 | -- dir = "editorconfig-core-lua-" .. _version, 9 | url = "git://github.com/editorconfig/editorconfig-core-lua.git", 10 | } 11 | description = { 12 | summary = "EditorConfig support for the Lua language", 13 | detailed = "EditorConfig makes it easy to maintain the correct \z 14 | coding style when switching between different text editors and \z 15 | betweendifferent projects. The EditorConfig project maintains a file \z 16 | format and plugins for various text editors which allow this file \z 17 | format to be read and used by those editors. EditorConfig Lua Core \z 18 | provides the same functionality as the Editorconfig C Core library.", 19 | homepage = "https://editorconfig.org", 20 | license = "BSD", 21 | } 22 | dependencies = { 23 | "lua >= 5.2", 24 | } 25 | build = { 26 | type = "cmake", 27 | variables = { 28 | CMAKE_BUILD_TYPE = "Release", 29 | CMAKE_INSTALL_PREFIX = "$(PREFIX)", 30 | ECL_LIBDIR = "$(LIBDIR)", 31 | ENABLE_TESTS = "Off", 32 | }, 33 | } 34 | -------------------------------------------------------------------------------- /editorconfig_lua.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 João Valverde 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | * POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "lua.h" 33 | #include "lauxlib.h" 34 | 35 | #ifndef ECL_VERSION 36 | #error "ECL_VERSION is not defined." 37 | #endif 38 | 39 | /*** 40 | * Lua bindings to the EditorConfig C Core library. 41 | * @module editorconfig 42 | */ 43 | 44 | /* Receives 3 arguments, two optional */ 45 | static editorconfig_handle 46 | open_ec_handle(lua_State *L) 47 | { 48 | const char *file_full_path; 49 | const char *conf_file_name; 50 | const char *version_to_set; 51 | int major = -1, minor = -1, patch = -1; 52 | editorconfig_handle eh; 53 | int err_num; 54 | 55 | file_full_path = luaL_checkstring(L, 1); 56 | conf_file_name = luaL_optstring(L, 2, NULL); 57 | version_to_set = luaL_optstring(L, 3, NULL); 58 | eh = editorconfig_handle_init(); 59 | if (eh == NULL) { 60 | luaL_error(L, "not enough memory to create handle"); 61 | } 62 | if (conf_file_name != NULL) { 63 | editorconfig_handle_set_conf_file_name(eh, conf_file_name); 64 | } 65 | if (version_to_set != NULL) { 66 | sscanf(version_to_set, "%d.%d.%d", &major, &minor, &patch); 67 | editorconfig_handle_set_version(eh, major, minor, patch); 68 | } 69 | err_num = editorconfig_parse(file_full_path, eh); 70 | if (err_num != 0) { 71 | const char *err_msg = editorconfig_get_error_msg(err_num); 72 | if (err_num > 0) { 73 | const char *err_file = editorconfig_handle_get_err_file(eh); 74 | luaL_error(L, "'%s' at line %d: %s", 75 | err_file ? err_file : "", err_num, err_msg); 76 | } 77 | luaL_error(L, "%s", err_msg); 78 | } 79 | return eh; 80 | } 81 | 82 | /*** 83 | * Load EditorConfig settings for a file. 84 | * @function parse 85 | * @string path full path to source file 86 | * @string[opt] config configuration file name 87 | * @string[opt] version version to set as a string tripe 88 | * @treturn {[name]=value,...} a property table 89 | * @treturn {name,...} an array with property names 90 | */ 91 | static int 92 | lec_parse(lua_State *L) 93 | { 94 | editorconfig_handle eh; 95 | int name_value_count; 96 | const char *name, *value; 97 | lua_Integer idx = 1; 98 | 99 | eh = open_ec_handle(L); 100 | assert(eh != NULL); 101 | lua_settop(L, 0); 102 | name_value_count = editorconfig_handle_get_name_value_count(eh); 103 | lua_createtable(L, 0, name_value_count); 104 | lua_createtable(L, name_value_count, 0); 105 | for (int i = 0; i < name_value_count; i++) { 106 | name = value = NULL; 107 | editorconfig_handle_get_name_value(eh, i, &name, &value); 108 | lua_pushstring(L, name); 109 | lua_pushstring(L, value); 110 | lua_settable(L, 1); 111 | lua_pushinteger(L, idx); 112 | lua_pushstring(L, name); 113 | lua_settable(L, 2); 114 | idx += 1; 115 | } 116 | editorconfig_handle_destroy(eh); 117 | return 2; 118 | } 119 | 120 | /*** 121 | * Module version 122 | * @field _VERSION string "EditorConfig Lua Core version <major.minor.patch>" 123 | */ 124 | /*** 125 | * EditorConfig C Core version 126 | * @field _C_VERSION string "EditorConfig C Core version <major.minor.patch>" 127 | */ 128 | static void 129 | add_version(lua_State *L) 130 | { 131 | int major = 0, minor = 0, patch = 0; 132 | const char *fmt; 133 | 134 | fmt = "EditorConfig Lua Core Version %s"; 135 | lua_pushfstring(L, fmt, ECL_VERSION); 136 | lua_setfield(L, -2, "_VERSION"); 137 | editorconfig_get_version(&major, &minor, &patch); 138 | fmt = "EditorConfig C Core Version %d.%d.%d"; 139 | lua_pushfstring(L, fmt, major, minor, patch); 140 | lua_setfield(L, -2, "_C_VERSION"); 141 | } 142 | 143 | static const struct luaL_Reg editorconfig_reg[] = { 144 | {"parse", lec_parse}, 145 | {NULL, NULL} 146 | }; 147 | 148 | int luaopen_editorconfig (lua_State *L) { 149 | luaL_newlib(L, editorconfig_reg); 150 | add_version(L); 151 | return 1; 152 | } 153 | -------------------------------------------------------------------------------- /main.lua: -------------------------------------------------------------------------------- 1 | 2 | require("pl") 3 | ec = require("editorconfig") 4 | 5 | local progname = arg[0] 6 | 7 | local function print_usage(ret) 8 | print(ec._VERSION) 9 | print("Usage: " .. progname .. " [OPTIONS] FILEPATH1 [FILEPATH2 FILEPATH3 ...]") 10 | -- print("FILEPATH can be a hyphen (-) if you want to path(s) to be read from stdin.") 11 | print() 12 | print("-f Specify conf filename other than \".editorconfig\".") 13 | print("-b Specify version (used by devs to test compatibility).") 14 | print("-h OR --help Print this help message.") 15 | print("-v OR --version Display version information.") 16 | os.exit(ret) 17 | end 18 | 19 | flags, params = app.parse_args(arg, {f = true, b = true}) 20 | 21 | if flags.h or flags.help then print_usage(0) end 22 | if flags.v or flags.version then print(ec._VERSION); os.exit(0) end 23 | if #params == 0 then print_usage(1) end 24 | 25 | local function run_editorconfig(path, show_header) 26 | if show_header then 27 | utils.printf("[%s]\n", path) 28 | end 29 | local props, names = ec.parse(path, flags.f, flags.b) 30 | for _, k in ipairs(names) do 31 | utils.printf("%s=%s\n", k, props[k]) 32 | end 33 | end 34 | 35 | local show_header = not (#params == 1) 36 | for _, path in ipairs(params) do 37 | run_editorconfig(path, show_header) 38 | end 39 | -------------------------------------------------------------------------------- /test.lua.in: -------------------------------------------------------------------------------- 1 | #!/bin/env lua 2 | 3 | package.path = "@CMAKE_SOURCE_DIR@/?.lua;" .. package.path 4 | package.cpath = "@CMAKE_BINARY_DIR@/?.so;" .. package.cpath 5 | 6 | dofile("@CMAKE_SOURCE_DIR@/main.lua") 7 | --------------------------------------------------------------------------------