nvim_conf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +98 -0
  4. data/bin/nvim_conf +13 -0
  5. data/lib/nvim_conf/compiler_configurations/compiler_configuration.rb +12 -0
  6. data/lib/nvim_conf/compiler_configurations/manager.rb +74 -0
  7. data/lib/nvim_conf/configuration_builder.rb +37 -0
  8. data/lib/nvim_conf/core.rb +56 -0
  9. data/lib/nvim_conf/generators/code/mappings/lua.rb +54 -0
  10. data/lib/nvim_conf/generators/code/mappings/vim.rb +27 -0
  11. data/lib/nvim_conf/generators/code/plugins/packer.rb +76 -0
  12. data/lib/nvim_conf/generators/code/settings/lua.rb +53 -0
  13. data/lib/nvim_conf/generators/code/settings/vim.rb +47 -0
  14. data/lib/nvim_conf/generators/generator.rb +6 -0
  15. data/lib/nvim_conf/manager.rb +6 -0
  16. data/lib/nvim_conf/mappings/manager.rb +58 -0
  17. data/lib/nvim_conf/mappings/mapping.rb +13 -0
  18. data/lib/nvim_conf/plugins/manager.rb +41 -0
  19. data/lib/nvim_conf/plugins/plugin.rb +19 -0
  20. data/lib/nvim_conf/settings/manager.rb +43 -0
  21. data/lib/nvim_conf/settings/setting.rb +14 -0
  22. data/lib/nvim_conf/version.rb +5 -0
  23. data/lib/nvim_conf/writer.rb +87 -0
  24. data/lib/nvim_conf/writers/code/mappings.rb +39 -0
  25. data/lib/nvim_conf/writers/code/orchestrator.rb +55 -0
  26. data/lib/nvim_conf/writers/code/plugins.rb +86 -0
  27. data/lib/nvim_conf/writers/code/settings.rb +38 -0
  28. data/lib/nvim_conf/writers/documentation/mappings.rb +66 -0
  29. data/lib/nvim_conf/writers/documentation/orchestrator.rb +44 -0
  30. data/lib/nvim_conf/writers/documentation/settings.rb +71 -0
  31. data/lib/nvim_conf.rb +33 -0
  32. metadata +77 -0
@@ -0,0 +1,41 @@
1
+ require "nvim_conf/plugins/plugin"
2
+
3
+ module NvimConf
4
+ module Plugins
5
+ class Manager < NvimConf::Manager
6
+ attr_reader :plugins, :name, :bootstraped
7
+
8
+ def initialize(name, bootstraped: false)
9
+ @name = name
10
+ @bootstraped = bootstraped
11
+ @plugins = []
12
+ end
13
+
14
+ def store?
15
+ @plugins.any?
16
+ end
17
+
18
+ def plug(name, **params)
19
+ store_plugin(
20
+ name,
21
+ params
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ def store_plugin(name, params)
28
+ @plugins << build_plugin(
29
+ name, params
30
+ )
31
+ end
32
+
33
+ def build_plugin(name, params)
34
+ Plugin.new(
35
+ name,
36
+ **params
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ require "dry-initializer"
2
+
3
+ module NvimConf
4
+ module Plugins
5
+ class Plugin
6
+ extend Dry::Initializer
7
+
8
+ param :name
9
+ option :loader, default: -> { :git }
10
+ option :provider, default: -> { :github }
11
+ option :branch, optional: true
12
+ option :as, optional: true
13
+ option :opt, optional: true
14
+ option :file_types, optional: true
15
+ option :cmd, optional: true
16
+ option :run, optional: true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ require "nvim_conf/settings/setting"
2
+
3
+ module NvimConf
4
+ module Settings
5
+ class Manager < NvimConf::Manager
6
+ attr_reader :settings
7
+
8
+ def initialize
9
+ @settings = []
10
+ end
11
+
12
+ def store?
13
+ @settings.any?
14
+ end
15
+
16
+ def set(key, value = nil, **params)
17
+ store_setting(
18
+ :set,
19
+ key: key,
20
+ value: value,
21
+ **params
22
+ )
23
+ end
24
+
25
+ def unset(key)
26
+ store_setting(
27
+ :unset,
28
+ key: key
29
+ )
30
+ end
31
+
32
+ private
33
+
34
+ def store_setting(operation, key:, value: nil, scope: :global)
35
+ @settings << build_setting(operation, key, value, scope)
36
+ end
37
+
38
+ def build_setting(operation, key, value, scope)
39
+ Setting.new(operation, key, value, scope)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,14 @@
1
+ require "dry-initializer"
2
+
3
+ module NvimConf
4
+ module Settings
5
+ class Setting
6
+ extend Dry::Initializer
7
+
8
+ param :operation
9
+ param :key
10
+ param :value, optional: true
11
+ param :scope, default: -> { :global }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NvimConf
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,87 @@
1
+ require "fileutils"
2
+
3
+ module NvimConf
4
+ class Writer
5
+ def initialize(managers)
6
+ @managers = managers
7
+ @configuration = NvimConf::ConfigurationBuilder.new(managers).build_configuration
8
+ end
9
+
10
+ def write
11
+ return unless run?
12
+
13
+ create_folder
14
+ write_configuration
15
+ write_documentation
16
+ end
17
+
18
+ private
19
+
20
+ def run?
21
+ @configuration[:write]
22
+ end
23
+
24
+ def write_documentation
25
+ create_documentation_init
26
+
27
+ File.open(documentation_file_path, "w+") do |file|
28
+ NvimConf::Writers::Documentation::Orchestrator.new(
29
+ @managers,
30
+ file,
31
+ @configuration
32
+ ).aggregate_writes
33
+ end
34
+ end
35
+
36
+ def create_folder
37
+ FileUtils.mkdir_p(folder_path)
38
+ end
39
+
40
+ def create_documentation_init
41
+ FileUtils.remove_file(
42
+ documentation_file_path,
43
+ force: true
44
+ )
45
+ FileUtils.touch(
46
+ documentation_file_path
47
+ )
48
+ end
49
+
50
+ def create_init
51
+ FileUtils.remove_file(
52
+ init_file_path,
53
+ force: true
54
+ )
55
+ FileUtils.touch(
56
+ init_file_path
57
+ )
58
+ end
59
+
60
+ def write_configuration
61
+ create_init
62
+ File.open(init_file_path, "w+") do |file|
63
+ NvimConf::Writers::Code::Orchestrator.new(
64
+ @managers,
65
+ file,
66
+ @configuration
67
+ ).aggregate_writes
68
+ end
69
+ end
70
+
71
+ def documentation_file_path
72
+ folder_path + "Init.md"
73
+ end
74
+
75
+ def init_file_path
76
+ folder_path + init_file_name
77
+ end
78
+
79
+ def init_file_name
80
+ @configuration[:schema] == :nvim ? "init.lua" : "init.vim"
81
+ end
82
+
83
+ def folder_path
84
+ @folder_path = Pathname.new(@configuration[:output_folder]).expand_path
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,39 @@
1
+ require "nvim_conf/generators/code/mappings/lua"
2
+ require "nvim_conf/generators/code/mappings/vim"
3
+
4
+ module NvimConf
5
+ module Writers
6
+ module Code
7
+ class MappingsWriter
8
+ MAPPING_GENERATOR_MAPPING = {
9
+ lua: Generators::Mappings::Code::Lua,
10
+ vim: Generators::Mappings::Code::Vim
11
+ }
12
+
13
+ def initialize(manager, io, format: :lua, commented: false)
14
+ @manager = manager
15
+ @io = io
16
+ @format = format
17
+ @commented = commented
18
+ end
19
+
20
+ def write
21
+ @manager.mappings.each do |mapping|
22
+ @io.write(
23
+ [
24
+ generator_class.new(mapping).generate,
25
+ "\n"
26
+ ].join
27
+ )
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def generator_class
34
+ MAPPING_GENERATOR_MAPPING[@format]
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ require "nvim_conf/writers/code/settings"
2
+ require "nvim_conf/writers/code/mappings"
3
+ require "nvim_conf/writers/code/plugins"
4
+
5
+ module NvimConf
6
+ module Writers
7
+ module Code
8
+ class Orchestrator
9
+ NON_WRITABLE_MANAGER = NvimConf::CompilerConfigurations::Manager
10
+
11
+ WRITER_CONFIGURATION = {
12
+ NvimConf::Settings::Manager => SettingsWriter,
13
+ NvimConf::Mappings::Manager => MappingsWriter,
14
+ NvimConf::Plugins::Manager => PluginsWriter
15
+ }
16
+
17
+ def initialize(managers, io, configuration = nil)
18
+ @managers = managers
19
+ @io = io
20
+ @configuration = configuration || default_configuration
21
+ end
22
+
23
+ def aggregate_writes
24
+ @managers.select { |manager| manager.class != NON_WRITABLE_MANAGER }.each_with_index do |manager, index|
25
+ if index.positive?
26
+ @io.write(
27
+ separator
28
+ )
29
+ end
30
+ aggregate_writes_for_manager(manager)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def separator
37
+ "\n\n"
38
+ end
39
+
40
+ def aggregate_writes_for_manager(manager)
41
+ WRITER_CONFIGURATION[manager.class]&.new(
42
+ manager, @io, **@configuration.slice(:format, :commented)
43
+ )&.write
44
+ end
45
+
46
+ def default_configuration
47
+ {
48
+ format: :lua,
49
+ commented: false
50
+ }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,86 @@
1
+ # TODO: This code needs to be refactored.... Bad CODE :D will be done during the addition of the second plugin manager!
2
+ module NvimConf
3
+ module Writers
4
+ module Code
5
+ class PluginsWriter
6
+ PLUGIN_MAP = {
7
+ packer: NvimConf::Generators::Plugins::Code::Packer
8
+ }
9
+
10
+ INDENT_MAP = {
11
+ packer: 2
12
+ }
13
+
14
+ def initialize(manager, io, _params)
15
+ @manager = manager
16
+ @io = io
17
+ end
18
+
19
+ def write
20
+ send("surround_#{@manager.name}") do |plugin|
21
+ @io.write(
22
+ [
23
+ plugin_indent(PLUGIN_MAP[@manager.name.to_sym].new(
24
+ plugin
25
+ ).generate),
26
+ "\n"
27
+ ].join
28
+ )
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def surround_packer(&block)
35
+ if bootstrap?
36
+ @io.write(
37
+ <<~BOOTSTRAP
38
+ local fn = vim.fn
39
+ local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
40
+ if fn.empty(fn.glob(install_path)) > 0 then
41
+ packer_bootstrap = fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
42
+ end\n
43
+ BOOTSTRAP
44
+ )
45
+ end
46
+
47
+ @io.write(
48
+ "return require('packer').startup(function()\n"
49
+ )
50
+
51
+ @manager.plugins.each(&block)
52
+
53
+ if bootstrap?
54
+ @io.write("\n")
55
+ @io.write(plugin_indent(
56
+ <<~BOOTSTRAP
57
+ if packer_bootstrap then
58
+ require('packer').sync()
59
+ end\n
60
+ BOOTSTRAP
61
+ ))
62
+ end
63
+
64
+ @io.write(
65
+ "end)\n"
66
+ )
67
+ end
68
+
69
+ def bootstrap?
70
+ @manager.bootstraped
71
+ end
72
+
73
+ def plugin_indent(content)
74
+ [
75
+ " " * INDENT_MAP[@manager.name.to_sym],
76
+ content
77
+ ].join
78
+ end
79
+
80
+ def generator_class
81
+ PLUGIN_MAP[@manager.name.to_sym]
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,38 @@
1
+ require "nvim_conf/generators/code/settings/lua"
2
+ require "nvim_conf/generators/code/settings/vim"
3
+
4
+ module NvimConf
5
+ module Writers
6
+ module Code
7
+ class SettingsWriter
8
+ SETTING_GENERATOR_MAPPING = {
9
+ lua: Generators::Settings::Code::Lua,
10
+ vim: Generators::Settings::Code::Vim
11
+ }
12
+
13
+ def initialize(manager, io, format:, commented:)
14
+ @manager = manager
15
+ @io = io
16
+ @format = format
17
+ end
18
+
19
+ def write
20
+ @manager.settings.each do |setting|
21
+ @io.write(
22
+ [
23
+ generator_class.new(setting).generate,
24
+ "\n"
25
+ ].join
26
+ )
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def generator_class
33
+ SETTING_GENERATOR_MAPPING[@format]
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,66 @@
1
+ module NvimConf
2
+ module Writers
3
+ module Documentation
4
+ class Mappings
5
+ MAIN_HEADER_PREFIX = "##"
6
+ MAPPING_HEADER_PREFIX = "###"
7
+
8
+ def initialize(managers, io)
9
+ @managers = managers
10
+ @io = io
11
+ end
12
+
13
+ def aggregate_writes
14
+ return if @managers.empty?
15
+
16
+ @io.write(
17
+ title(
18
+ "Mappings",
19
+ MAIN_HEADER_PREFIX
20
+ )
21
+ )
22
+
23
+ write_mappings(all_mappings.group_by(&:operator))
24
+ end
25
+
26
+ private
27
+
28
+ def write_mappings(grouped_mappings)
29
+ grouped_mappings.each do |operator, mappings|
30
+ write_separator
31
+
32
+ @io.write(
33
+ title(
34
+ operator,
35
+ MAPPING_HEADER_PREFIX
36
+ )
37
+ )
38
+
39
+ mappings.each do |mapping|
40
+ @io.write(
41
+ [
42
+ "- #{mapping.binding}",
43
+ mapping.action
44
+ ].join(" => ") + "\n"
45
+ )
46
+ end
47
+ end
48
+ end
49
+
50
+ def all_mappings
51
+ @managers.map(&:mappings).flatten
52
+ end
53
+
54
+ def write_separator
55
+ @io.write(
56
+ "\n\n"
57
+ )
58
+ end
59
+
60
+ def title(text, prefix)
61
+ "#{prefix} #{text}\n"
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,44 @@
1
+ require "nvim_conf/writers/documentation/mappings"
2
+ require "nvim_conf/writers/documentation/settings"
3
+
4
+ module NvimConf
5
+ module Writers
6
+ module Documentation
7
+ class Orchestrator
8
+ NON_WRITABLE_MANAGER = NvimConf::CompilerConfigurations::Manager
9
+
10
+ def initialize(managers, io, configuration = nil)
11
+ @managers = managers
12
+ @io = io
13
+ @configuration = configuration || default_configuration
14
+ end
15
+
16
+ def aggregate_writes
17
+ return unless @configuration[:documented]
18
+
19
+ write_main_header
20
+
21
+ {
22
+ NvimConf::Writers::Documentation::Settings => NvimConf::Settings::Manager,
23
+ NvimConf::Writers::Documentation::Mappings => NvimConf::Mappings::Manager
24
+ }.each do |writer_class, manager_class|
25
+ writer_class.new(
26
+ @managers.select { |manager| manager.instance_of?(manager_class) },
27
+ @io
28
+ ).aggregate_writes
29
+
30
+ @io.write("\n\n")
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def write_main_header
37
+ @io.write(
38
+ "# Configuration Documentation Vim - NvimConf\n\n"
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,71 @@
1
+ module NvimConf
2
+ module Writers
3
+ module Documentation
4
+ class Settings
5
+ MAIN_HEADER_PREFIX = "##"
6
+
7
+ def initialize(managers, io)
8
+ @managers = managers
9
+ @io = io
10
+ end
11
+
12
+ def aggregate_writes
13
+ return if @managers.empty?
14
+
15
+ @io.write(
16
+ title(
17
+ "Settings",
18
+ MAIN_HEADER_PREFIX
19
+ )
20
+ )
21
+
22
+ write_settings_groups(all_settings.group_by(&:operation))
23
+ end
24
+
25
+ private
26
+
27
+ def write_settings_groups(groups)
28
+ groups.each do |operation, settings|
29
+ write_separator
30
+
31
+ @io.write(
32
+ title(
33
+ operation.capitalize,
34
+ "###"
35
+ )
36
+ )
37
+
38
+ settings.each do |setting|
39
+ @io.write(
40
+ [
41
+ "- #{setting.key}",
42
+ transformed_value(setting, setting.value)
43
+ ].join(" => ") + "\n"
44
+ )
45
+ end
46
+ end
47
+ end
48
+
49
+ def transformed_value(setting, value)
50
+ return value unless value.nil?
51
+
52
+ setting.operation == :set
53
+ end
54
+
55
+ def all_settings
56
+ @managers.map(&:settings).flatten
57
+ end
58
+
59
+ def write_separator
60
+ @io.write(
61
+ "\n\n"
62
+ )
63
+ end
64
+
65
+ def title(text, prefix)
66
+ "#{prefix} #{text}\n"
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
data/lib/nvim_conf.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nvim_conf/version"
4
+ require "set"
5
+ require "nvim_conf/core"
6
+ require "nvim_conf/writer"
7
+ require "nvim_conf/manager"
8
+ require "nvim_conf/configuration_builder"
9
+ require "nvim_conf/plugins/manager"
10
+ require "nvim_conf/settings/manager"
11
+ require "nvim_conf/mappings/manager"
12
+ require "nvim_conf/compiler_configurations/manager"
13
+ require "nvim_conf/generators/code/settings/lua"
14
+ require "nvim_conf/generators/code/plugins/packer"
15
+ require "nvim_conf/generators/code/settings/vim"
16
+ require "nvim_conf/generators/code/mappings/lua"
17
+ require "nvim_conf/generators/code/mappings/vim"
18
+ require "nvim_conf/writers/code/orchestrator"
19
+ require "nvim_conf/writers/documentation/orchestrator"
20
+
21
+ module NvimConf
22
+ @managers = []
23
+
24
+ def self.managers
25
+ @managers
26
+ end
27
+
28
+ def self.clear_managers
29
+ @managers = []
30
+ end
31
+
32
+ class Error < StandardError; end
33
+ end