Skip to content

Commit

Permalink
editorconfig.vala: make hook document handler asynchronous (#1334)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeremy Wootten <[email protected]>
Co-authored-by: Jeremy Wootten <[email protected]>
Co-authored-by: Ryan Kornheisl <[email protected]>
  • Loading branch information
4 people authored Mar 7, 2025
1 parent 3346c82 commit c96f526
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions plugins/editorconfig/editorconfig.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,60 +32,65 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa
});

plugins.hook_document.connect ((d) => {
// Ensure use global settings by default
format_bar.tab_style_set_by_editor_config = false;
format_bar.tab_width_set_by_editor_config = false;
format_bar.set_document (d);
update_config.begin (d);
});

Scratch.Widgets.SourceView view = d.source_view;
File file = d.file;
}

if (file == null || !file.query_exists ()) {
return;
}
private async void update_config (Scratch.Services.Document d) {
// Ensure use global settings by default
format_bar.tab_style_set_by_editor_config = false;
format_bar.tab_width_set_by_editor_config = false;
format_bar.set_document (d);

var handle = new EditorConfig.Handle ();
handle.set_conf_file_name (".editorconfig");
if (handle.parse (file.get_path ()) != 0) {
return;
}
Scratch.Widgets.SourceView view = d.source_view;
File file = d.file;

for (int i = 0; i < handle.get_name_value_count (); i++) {
string name, val;
handle.get_name_value (i, out name, out val);
/* These are all properties (https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties) */
switch (name) {
case "indent_style":
format_bar.tab_style_set_by_editor_config = true;
var use_spaces = (val != "tab");
format_bar.set_insert_spaces_instead_of_tabs (use_spaces);
break;
case "indent_size":
case "tab_width":
format_bar.tab_width_set_by_editor_config = true;
var indent_width = (int.parse (val)).clamp (2, 16);
format_bar.set_tab_width (indent_width);
break;
case "end_of_line":
break;
case "charset":
break;
case "trim_trailing_whitespace":
break;
case "insert_final_newline":
break;
case "max_line_length":
view.right_margin_position = int.parse (val);
break;
default:
warning ("unrecognised name/value %s/%s", name, val);
break;
}
if (file == null || !file.query_exists ()) {
return;
}

var handle = new EditorConfig.Handle ();
handle.set_conf_file_name (".editorconfig");
if (handle.parse (file.get_path ()) != 0) {
return;
}

for (int i = 0; i < handle.get_name_value_count (); i++) {
string name, val;
handle.get_name_value (i, out name, out val);
/* These are all properties (https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties) */
switch (name) {
case "indent_style":
format_bar.tab_style_set_by_editor_config = true;
var use_spaces = (val != "tab");
format_bar.set_insert_spaces_instead_of_tabs (use_spaces);
break;
case "indent_size":
case "tab_width":
format_bar.tab_width_set_by_editor_config = true;
var indent_width = (int.parse (val)).clamp (2, 16);
format_bar.set_tab_width (indent_width);
break;
case "end_of_line":
break;
case "charset":
break;
case "trim_trailing_whitespace":
break;
case "insert_final_newline":
break;
case "max_line_length":
view.right_margin_position = int.parse (val);
break;
default:
warning ("unrecognised name/value %s/%s", name, val);
break;
}
});
}
}

public void deactivate () { }
public void deactivate () { debug ("Editor config deactivate");}
}

[ModuleInit]
Expand Down

0 comments on commit c96f526

Please sign in to comment.