Module: Canopus::Workspace::SettingsAware

Defined in:
lib/canopus/workspace/settings_aware.rb

Instance Method Summary collapse

Instance Method Details

#apply_settings ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/canopus/workspace/settings_aware.rb', line 40

def apply_settings
  reset_auto_save
  cancel_workspace_symbol_search
  self.palette = nil if @palette&.dig(:kind) == :workspace_symbol_results
  servers = language_server_settings_plan
  clear_vim_states unless @settings["vim_mode"]
  name = @settings["theme"]
  if name == "auto"
    name = @window&.respond_to?(:appearance) && @window.appearance == :light ? "Canopus Light" : "Canopus Dark"
  end
  target_theme = File.file?(File.expand_path(name, @root)) ? Theme.load(File.expand_path(name, @root)) : Theme.new(name: name)
  if @applied_icon_theme != @settings["icon_theme"]
    require_relative "../icon_theme"
    @icon_theme = IconTheme.new(@settings["icon_theme"] && File.expand_path(@settings["icon_theme"], @root))
  end
  @panes.each do |pane|
    pane.editors.each do |current|
      apply_editor_settings(current)
    end
  end
  self.theme = target_theme
  if @window&.text_system && !@window.is_a?(Zaniah::Platform::TUI::Window) && @applied_font_family != @settings["font_family"]
    configure_text_system
  end
  @applied_font_family = @settings["font_family"]
  @applied_icon_theme = @settings["icon_theme"]
  invalidate_diagnostics
  invalidate_document_highlights
  invalidate_document_links
  invalidate_folding_ranges
  invalidate_selection_ranges
  invalidate_hierarchy
  invalidate_prepare_rename
  invalidate_linked_editing_ranges
  invalidate_inlay_hints
  invalidate_code_lenses
  invalidate_brackets
  @decorations.invalidate(:blame)
  reload_language_servers(servers)
  @window&.request_frame
end

#configure_text_system(cache_dir: @window.text_system&.cache_dir) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/canopus/workspace/settings_aware.rb', line 31

def configure_text_system(cache_dir: @window.text_system&.cache_dir)
  database = Zaniah::TextSystem::FontDB.new
  system = Zaniah::TextSystem::Renderer.new(font_db: database, font: database.find(family: @settings["font_family"]), cache_dir: cache_dir)
  system.scale_factor = @window.scale_factor
  old, @window.text_system = @window.text_system, system
  old&.close
  @applied_font_family = @settings["font_family"]
  system
end

#icon_theme ⇒ Object



5
6
7
8
# File 'lib/canopus/workspace/settings_aware.rb', line 5

def icon_theme
  require_relative "../icon_theme"
  @icon_theme ||= IconTheme.new(@settings["icon_theme"] && File.expand_path(@settings["icon_theme"], @root))
end

#open_settings ⇒ Object



10
11
12
13
14
15
16
# File 'lib/canopus/workspace/settings_aware.rb', line 10

def open_settings
  FileUtils.mkdir_p(File.dirname(settings_path))
  current = open(settings_path)
  current.buffer.edit([[0...0, "// Project settings override user settings.\n{\n  \"tab_size\": #{@settings['tab_size']}\n}\n"]], kind: :settings) if current.buffer.rope.empty?
  @message = "Edit JSONC and save; Ctrl-Space completes setting names"
  current
end

#poll_settings(force: false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/canopus/workspace/settings_aware.rb', line 81

def poll_settings(force: false)
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  return if !force && @last_settings_poll && now - @last_settings_poll < 1
  @last_settings_poll = now
  state = @settings.paths.map do |path|
    stat = File.stat(path)
    [path, stat.mtime, stat.size, stat.ino]
  rescue Errno::ENOENT
    [path, nil]
  end
  return if state == @settings_state && !force
  initial = !defined?(@settings_state)
  @settings_state = state
  return if initial && !force
  previous_paths = @settings.paths
  replacement = @settings.reload
  old, @settings = @settings, replacement
  publish_settings_diagnostics(previous_paths | @settings.paths)
  apply_settings
  errors = @settings.paths.filter_map { |path| Settings.file_diagnostics(path)&.last }.flatten
  @message = errors.empty? ? "Settings reloaded" : "Settings unchanged: #{errors.first.message}"
  notify_plugin_event("settings/didChange", {"paths" => @settings.paths})
rescue StandardError => error
  @settings = old if old
  publish_settings_diagnostics(@settings.paths) if @settings
  @message = "Settings unchanged: #{error.message}"
end

#settings_browse(changed_only: false) ⇒ Object



21
22
23
24
25
26
# File 'lib/canopus/workspace/settings_aware.rb', line 21

def settings_browse(changed_only: false)
  fields = settings_gui_fields(changed_only: changed_only)
  labels = fields.map { |field| "#{field[:path].join(".")} = #{JSON.generate(field[:value])}" }
  self.palette = {kind: :settings_gui, query: +"", index: 0, matches: labels, fields: fields,
    settings_file: settings_path, changed_only: changed_only}
end

#settings_completions ⇒ Object

Raises:



17
18
19
20
# File 'lib/canopus/workspace/settings_aware.rb', line 17

def settings_completions
  raise Error, "Open a settings document first" unless settings_document?(editor.buffer)
  self.palette = {kind: :settings_keys, query: +"", index: 0, matches: Settings::DEFAULTS.keys}
end

#settings_document?(buffer) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/canopus/workspace/settings_aware.rb', line 28

def settings_document?(buffer)
  buffer.path && (buffer.path == settings_path || @settings.paths.any? { |path| File.expand_path(path) == buffer.path })
end

#settings_gui(changed_only: false) ⇒ Object



27
# File 'lib/canopus/workspace/settings_aware.rb', line 27

def settings_gui(changed_only: false) = settings_browse(changed_only: changed_only)

#settings_path ⇒ Object



9
# File 'lib/canopus/workspace/settings_aware.rb', line 9

def settings_path = File.join(@root, ".canopus", "settings.jsonc")