Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions data/org.x.editor.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@
<description>Whether xed should enable syntax highlighting.</description>
</key>

<key name="max-file-size-for-highlighting" type="u">
<default>4</default>
<summary>Maximum file size for syntax highlighting</summary>
<description>Files larger than this size (in MB) will be opened without syntax highlighting and without search highlighting, for performance reasons. Use 0 to disable this limit.</description>
</key>

<key name="ensure-trailing-newline" type="b">
<default>true</default>
<summary>Ensure Trailing Newline</summary>
Expand Down
46 changes: 46 additions & 0 deletions xed/resources/ui/xed-preferences-dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkAdjustment" id="adjustment4">
<property name="lower">0</property>
<property name="upper">1000</property>
<property name="value">4</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkBox" id="editor_page">
<property name="visible">True</property>
<property name="can_focus">False</property>
Expand Down Expand Up @@ -617,6 +624,45 @@
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box31">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label30">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Disable syntax highlighting for files larger than (MB, 0 = no limit)</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="max_file_size_for_highlighting_spin">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="text" translatable="yes">4</property>
<property name="adjustment">adjustment4</property>
<property name="value">4</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
Expand Down
56 changes: 56 additions & 0 deletions xed/xed-document.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,11 +1118,36 @@ loaded_query_info_cb (GFile *location,
g_object_unref (doc);
}

/* Returns the file size limit (in characters) above which highlighting is
* disabled for performance reasons, or -1 if there is no limit. The limit
* is user-configurable via the "max-file-size-for-highlighting" setting
* (expressed in MB there; 0 means "no limit").
*/
static gint64
get_max_char_count_for_highlighting (XedDocument *doc)
{
XedDocumentPrivate *priv;
guint max_size_mb;

priv = xed_document_get_instance_private (doc);

max_size_mb = g_settings_get_uint (priv->editor_settings, XED_SETTINGS_MAX_FILE_SIZE_FOR_HIGHLIGHTING);

if (max_size_mb == 0)
{
return -1;
}

return (gint64) max_size_mb * 1000000;
}

static void
xed_document_loaded_real (XedDocument *doc)
{
XedDocumentPrivate *priv;
GFile *location;
gint64 max_chars;
gint char_count;

priv = xed_document_get_instance_private (doc);

Expand All @@ -1136,6 +1161,21 @@ xed_document_loaded_real (XedDocument *doc)
set_language (doc, language, FALSE);
}

max_chars = get_max_char_count_for_highlighting (doc);
char_count = gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (doc));

if (gtk_source_buffer_get_highlight_syntax (GTK_SOURCE_BUFFER (doc)) &&
max_chars >= 0 && char_count > max_chars)
{
xed_debug_message (DEBUG_DOCUMENT,
"File has %d characters, above the %" G_GINT64_FORMAT
"-character limit (see the max-file-size-for-highlighting setting): "
"disabling syntax highlighting for performance",
char_count, max_chars);

gtk_source_buffer_set_highlight_syntax (GTK_SOURCE_BUFFER (doc), FALSE);
}

g_get_current_time (&priv->time_of_last_save_or_load);

xed_document_set_content_type (doc, NULL);
Expand Down Expand Up @@ -1619,6 +1659,22 @@ xed_document_set_search_context (XedDocument *doc,
{
gboolean highlight = g_settings_get_boolean (priv->editor_settings, XED_SETTINGS_SEARCH_HIGHLIGHTING);

if (highlight)
{
gint64 max_chars = get_max_char_count_for_highlighting (doc);
gint char_count = gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (doc));

if (max_chars >= 0 && char_count > max_chars)
{
xed_debug_message (DEBUG_DOCUMENT,
"File has %d characters, above the %" G_GINT64_FORMAT
"-character limit (see the max-file-size-for-highlighting setting): "
"disabling search highlighting for performance",
char_count, max_chars);
highlight = FALSE;
}
}

gtk_source_search_context_set_highlight (search_context, highlight);

g_object_ref (search_context);
Expand Down
10 changes: 10 additions & 0 deletions xed/xed-preferences-dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ struct _XedPreferencesDialog
/* Highlight matching bracket */
GtkWidget *highlight_matching_bracket_switch;

/* Max file size for highlighting */
GtkWidget *max_file_size_for_highlighting_spin;

/* Tabs */
GtkWidget *tab_width_spin;
GtkWidget *use_spaces_switch;
Expand Down Expand Up @@ -188,6 +191,7 @@ xed_preferences_dialog_class_init (XedPreferencesDialogClass *klass)
gtk_widget_class_bind_template_child (widget_class, XedPreferencesDialog, draw_whitespace_newline_switch);
gtk_widget_class_bind_template_child (widget_class, XedPreferencesDialog, highlight_current_line_switch);
gtk_widget_class_bind_template_child (widget_class, XedPreferencesDialog, highlight_matching_bracket_switch);
gtk_widget_class_bind_template_child (widget_class, XedPreferencesDialog, max_file_size_for_highlighting_spin);
gtk_widget_class_bind_template_child (widget_class, XedPreferencesDialog, tab_width_spin);
gtk_widget_class_bind_template_child (widget_class, XedPreferencesDialog, use_spaces_switch);
gtk_widget_class_bind_template_child (widget_class, XedPreferencesDialog, automatic_indentation_switch);
Expand Down Expand Up @@ -386,6 +390,12 @@ setup_editor_page (XedPreferencesDialog *dlg)
"active",
G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);

g_settings_bind (dlg->editor_settings,
XED_SETTINGS_MAX_FILE_SIZE_FOR_HIGHLIGHTING,
dlg->max_file_size_for_highlighting_spin,
"value",
G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);

/* Indentation */
g_settings_bind (dlg->editor_settings,
XED_SETTINGS_TABS_SIZE,
Expand Down
1 change: 1 addition & 0 deletions xed/xed-settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void xed_settings_set_list (GSettings *settings,
#define XED_SETTINGS_RESTORE_CURSOR_POSITION "restore-cursor-position"
#define XED_SETTINGS_SYNTAX_HIGHLIGHTING "syntax-highlighting"
#define XED_SETTINGS_SEARCH_HIGHLIGHTING "search-highlighting"
#define XED_SETTINGS_MAX_FILE_SIZE_FOR_HIGHLIGHTING "max-file-size-for-highlighting"
#define XED_SETTINGS_ENABLE_TAB_SCROLLING "enable-tab-scrolling"
#define XED_SETTINGS_AUTO_CLOSE "auto-close"
#define XED_SETTINGS_TOOLBAR_VISIBLE "toolbar-visible"
Expand Down