Git Product home page Git Product logo

vis-editorconfig's Introduction

vis-editorconfig

A vis plugin for editorconfig.

Installation

You'll need the Lua wrapper for editorconfig-core installed. This can be done through luarocks: luarocks install editorconfig-core

git clone https://github.com/seifferth/vis-editorconfig "$HOME/.config/vis/edconf"

Then add require('edconf') to your visrc.lua.

You may use a different name for the local repository, if you like. You could, for instance, use $HOME/.config/vis/vis-editorconfig and require('vis-editorconfig'). Note, however, that the repository must not be called editorconfig. Since the editorconfig-core lua library is also called editorconfig, naming the repository editorconfig will cause name conflicts that result in infinite recursion.

Functionality

Not all editorconfig functionality is supported by vis and hence by this plugin. At this moment, there is full support for the following settings:

  • indent_style
  • indent_size
  • tab_width
  • insert_final_newline

The following settings are implemented partially and / or support is turned off by default:

  • spell_language: This is not yet part of the editorconfig specification (cf. editorconfig/editorconfig#315), but it is implemented anyway. Since vis does not support spellchecking natively, this plugin will only set vis.win.file.spell_language to the specified value. It is then up to the spellchecking plugin to respect that variable.
  • trim_trailing_whitespace: Turned off by default, can be enabled via :set edconfhooks on.
  • end_of_line: Turned off by default, partial support can be enabled via :set edconfhooks on. Only crlf and lf are supported, plain cr is not. The implementation is also very basic. If end_of_line is set to crlf, a return character will be inserted at the end of each line that does not yet end with crlf. If end_of_line is set to lf, return characters at the end of a line will be stripped. While I would encourage every vis user to stick to lf terminated files, this might be convenient if, for some reason, they do need to compose crlf terminated files.
  • max_line_length: Turned off by default, partial support can be enabled via :set edconfhooks on. There is no straightforward way to automatically wrap content that might be written in arbitrary programming (or non-programming) languages. For that reason, vis-editorconfig doesn't even try. If max_line_length is enabled, vis-editorconfig issues a warning, however, indicating which lines are longer than the specified length. Note that you will miss this warning if you write your file with :wq, so if you care about it, write it with :w<RETURN>:q.

The reason those last three settings are optional and turned off by default is their scalability. Each of those operations is implemented as a pre-save-hook with a complexity of O(n), where n is the filesize. Since vis is incredibly good at editing huge files efficiently, there seems to be a very real danger that those hooks could cause the editor to freeze just before a user's valuable changes are written to disk.

You can turn support for those pre-save-hooks on or off at any time by running

:set edconfhooks on

or

:set edconfhooks off

If edconfhooks are enabled, they will be executed as configured in .editorconfig. If you want to take a less cautious approach and enable these hooks by default, simply add an additional line below the module import in visrc.lua:

require('editorconfig/edconf')
vis:command('set edconfhooks on')   -- supposing you did previously
                                    -- require('vis')

vis-editorconfig's People

Contributors

fischerling avatar raedwulf avatar seifferth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vis-editorconfig's Issues

Pressing the <Tab> key doesn't respect .editorconfig with tabwidth set

I'm not sure if this is intentional, but if I have vis:command('set tabwidth 4'), for example, and then have a .editorconfig with

[*]
indent_style = tab
tab_width = 2

the tabs already in the file are set to width 2, but hitting <tab> still generates a 4 width tab. This can all be avoided by just not setting tabwidth in visrc.lua, but it still seems like possibly incorrect behavior.

There are some other weird behaviors using indent_style = space, but I suspect that has to do with my vis config using tabs, and the file originally having tabs, but some converted to spaces or something. A fix for the original issue might affect/fix this anyway.

editorconfig_core file missing

Hello, I just tested this plugin for vis and the first thing that happens when I add it in my visrc.lua is that a file a missing: editorconfig_core.

Can you add the required file?

Thanks!

C stack overflow

Install vis-editorconfig according to the README, and got this error:

C stack overflow
C stack overflow
stack traceback:
        [C]: in function 'dofile'
        /home/xxx/.config/vis/editorconfig/init.lua:4: in main chunk
        [C]: in function 'require'
        /home/xxx/.config/vis/editorconfig/edconf.lua:2: in main chunk
        [C]: in function 'dofile'
        /home/xxx/.config/vis/editorconfig/init.lua:4: in main chunk
        [C]: in function 'require'
        /home/xxx/.config/vis/editorconfig/edconf.lua:2: in main chunk
        [C]: in function 'dofile'
        /home/xxx/.config/vis/editorconfig/init.lua:4: in main chunk
        ...     (skipping 361 levels)
        [C]: in function 'dofile'
        /home/xxx/.config/vis/editorconfig/init.lua:4: in main chunk
        [C]: in function 'require'
        /home/xxx/.config/vis/editorconfig/edconf.lua:2: in main chunk
        ...     (skipping 361 levels)
        [C]: in function 'dofile'
        /home/xxx/.config/vis/editorconfig/init.lua:4: in mai
n chunk
        [C]: in function 'require'
        /home/xxx/.config/vis/editorconfig/edconf.lua:2: in m
ain chunk
        [C]: in function 'dofile'
        /home/xxx/.config/vis/editorconfig/init.lua:4: in mai
n chunk
        [C]: in function 'require'
        /home/xxx/.config/vis/editorconfig/edconf.lua:2: in m
ain chunk
        [C]: in function 'require'
        /home/xxx/.config/vis/visrc.lua:3: in main chunk
        [C]: in function 'require'

It seems that it enters an infinite loops between init.lua and edconf.lua. I'm not so fimiliar with lua. Just to have some taste about sregex:)
Here is my visrc.lua:

-- load standard vis module, providing parts of the Lua API
require('vis')
require('editorconfig/edconf')

vis.events.subscribe(vis.events.INIT, function()
	-- global configuration options
end)

vis.events.subscribe(vis.events.WIN_OPEN, function(win)
	-- per window configuration options
end)

make vis-editoconfig compatible with vis 0.9

The upcoming vis release 0.9 makes some options (e.g. tabwidth) window-specific. See martanne/vis@450dc2d and martanne/vis@de315f8.

This breaks vis-editorconfig since it tries to set all options during the FILE_OPEN event.

I currently use this patch to support the current vis master branch.

diff --git a/edconf.lua b/edconf.lua
index 5ef0f1a..2ba0445 100644
--- a/edconf.lua
+++ b/edconf.lua
@@ -2,16 +2,12 @@ require "vis"
 ec = require "editorconfig"
 
 -- Simple wrapper
-function vis_set(option, value)
-  if type(value) == "boolean" then
-    if value then
-      value = "yes"
-    else
-      value = "no"
-    end
+function vis_set(option, value, win)
+  if win then
+    win.options[option] = value
+  else
+    vis.options[option] = value
   end
-
-  vis:command("set " .. option .. " " .. value)
 end
 
 function set_pre_save(f, value)
@@ -102,20 +98,6 @@ function max_line_length(file, path)
 end
 
 OPTIONS = {
-  indent_style = function (value)
-    vis_set("expandtab", (value == "space"))
-  end,
-
-  indent_size = function (value)
-    if value ~= "tab" then -- tab_width is a synonym anyway
-      vis_set("tabwidth", value)
-    end
-  end,
-
-  tab_width = function (value)
-    vis_set("tabwidth", value)
-  end,
-
   spelling_language = function (value, file)
     file.spelling_language = value
   end,
@@ -164,6 +146,22 @@ OPTIONS = {
   --   max_line_length
 }
 
+WIN_OPTIONS = {
+  indent_style = function (value)
+    vis_set("expandtab", (value == "space"), vis.win)
+  end,
+
+  indent_size = function (value)
+    if value ~= "tab" then -- tab_width is a synonym anyway
+      vis_set("tabwidth", value, vis.win)
+    end
+  end,
+
+  tab_width = function (value)
+    vis_set("tabwidth", value, vis.win)
+  end,
+}
+
 -- Compatible with editorconfig-core-lua v0.3.0
 function ec_iter(p)
   i = 0
@@ -187,8 +185,21 @@ function ec_set_values(file)
   end
 end
 
-function ec_parse_cmd() ec_set_values(vis.win.file) end
-vis:command_register("econfig_parse", ec_parse_cmd, "(Re)parse an editorconfig file")
+function ec_set_win_values(win)
+  if win.file.path then
+    for name, value in ec_iter(win.file.path) do
+      if WIN_OPTIONS[name] then
+        WIN_OPTIONS[name](value, win)
+      end
+    end
+  end
+end
+
+
+vis:command_register("econfig_parse", function()
+  ec_set_values(vis.win.file)
+  ec_set_win_values(vis.win)
+end, "(Re)parse an editorconfig file")
 
 vis.events.subscribe(vis.events.FILE_OPEN, function (file)
   ec_set_values(file)
@@ -198,6 +209,10 @@ vis.events.subscribe(vis.events.FILE_SAVE_POST, function (file, path)
   ec_set_values(file)
 end)
 
+vis.events.subscribe(vis.events.WIN_OPEN, function (win)
+  ec_set_win_values(win)
+end)
+
 edconf_hooks_enabled = false
 vis:option_register("edconfhooks", "bool", function(value)
   edconf_hooks_enabled = value

However my patch does fix ec_parse_cmd() only setting file only options.

Unrelated to the functional changes is the switch to the new lua options table
API.

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.