Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tab-size from EditorConfig #1239

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ jobs:
export LANG=en_US.utf8
sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt -yq install --no-install-recommends \
asciidoc valgrind xmlto
asciidoc \
libeditorconfig-dev \
valgrind \
xmlto
CC=${{ matrix.compiler }} TIG_BUILD=${{ matrix.tig_build }} tools/travis.sh
10 changes: 9 additions & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,13 @@ jobs:
- name: Test Tig
shell: 'script -q typescript sh {0}' # Workaround to get a TTY, see https://github.com/gfx/example-github-actions-with-tty
run: |
brew install asciidoc autoconf automake coreutils gnu-sed ncurses xmlto
brew install \
asciidoc \
autoconf \
automake \
coreutils \
editorconfig \
gnu-sed \
ncurses \
xmlto
TIG_BUILD=autoconf tools/travis.sh
2 changes: 2 additions & 0 deletions INSTALL.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ configure script and building documentation:
search and command prompts.
|PCRE |Adds support for Perl Compatible Regular
Expressions in searches.
|EditorConfig core library |Adds support for setting tab-size based on
an .editorconfig file.
|autoconf |Contains autoreconf for generating configure
from configure.ac.
|asciidoc (>= 8.4) |Generates HTML and (DocBook) XML from text.
Expand Down
7 changes: 7 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Release notes
=============

master
------

Improvements:

- Honor tab width from EditorConfig by linking against the EditorConfig core library. (#1239)

tig-2.5.8
---------

Expand Down
10 changes: 10 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ AS_IF([test "x$with_pcre" != xno], [
])
])

dnl Check for EditorConfig library
AC_ARG_WITH(editorconfig, [AS_HELP_STRING([--without-editorconfig], [do not use the EditorConfig library])])
AS_IF([test "x$with_editorconfig" != xno], [
AC_CHECK_HEADERS([editorconfig/editorconfig.h])
AS_IF([test "x$ac_cv_header_editorconfig_editorconfig_h" = xyes], [
AC_DEFINE([HAVE_EDITORCONFIG], [1], [Define if you have EditorConfig])
LIBS="$LIBS -leditorconfig"
])
])

dnl OS-specific
case $(uname -s 2>/dev/null || echo unknown) in "OS400")
AC_CHECK_LIB(util, main, [LIBS="$LIBS -lutil"], AC_MSG_ERROR([Please install the libutil-devel package]))
Expand Down
1 change: 1 addition & 0 deletions doc/tigrc.5.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ The following variables can be set:
'tab-size' (int)::

Number of spaces per tab. The default is 8 spaces.
This may be overridden by an EditorConfig file.

'diff-context' (int)::

Expand Down
9 changes: 9 additions & 0 deletions include/tig/diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@

#include "tig/view.h"

#if defined HAVE_EDITORCONFIG
struct diff_common_state {
uint8_t tab_size;
};
#endif

struct diff_state {
#if defined HAVE_EDITORCONFIG
struct diff_common_state common;
#endif
bool after_commit_title;
bool after_diff;
bool reading_diff_chunk;
Expand Down
2 changes: 1 addition & 1 deletion include/tig/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum align {
ALIGN_RIGHT
};

bool draw_text(struct view *view, enum line_type type, const char *string);
bool draw_text(struct view *view, enum line_type type, const char *string, int tab_size);
bool PRINTF_LIKE(3, 4) draw_formatted(struct view *view, enum line_type type, const char *format, ...);
bool draw_graphic(struct view *view, enum line_type type, const chtype graphic[], size_t size, bool separator);
bool draw_field(struct view *view, enum line_type type, const char *text, int width, enum align align, bool trim);
Expand Down
4 changes: 3 additions & 1 deletion include/tig/pager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#include "tig/view.h"

bool pager_get_column_data(struct view *view, const struct line *line, struct view_column_data *column_data);
bool pager_common_read(struct view *view, const char *data, enum line_type type, struct line **line);
bool pager_common_read(struct view *view, const char *data, enum line_type type, bool is_diff, struct line **line);
enum request pager_request(struct view *view, enum request request, struct line *line);
void pager_select(struct view *view, struct line *line);

uint8_t editorconfig_tab_size(const char file[]);

extern struct view pager_view;

static inline void
Expand Down
3 changes: 3 additions & 0 deletions include/tig/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct box {
struct line {
enum line_type type;
unsigned int lineno:24;
#if defined HAVE_EDITORCONFIG
unsigned int tab_size:8;
#endif

/* State flags */
unsigned int selected:1;
Expand Down
10 changes: 9 additions & 1 deletion src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "tig/refdb.h"
#include "tig/parse.h"
#include "tig/repo.h"
#include "tig/diff.h"
#include "tig/display.h"
#include "tig/draw.h"
#include "tig/ui.h"
Expand All @@ -22,6 +23,9 @@
#include "tig/blob.h"

struct blob_state {
#if defined HAVE_EDITORCONFIG
struct diff_common_state common;
#endif
char commit[SIZEOF_REF];
const char *file;
};
Expand Down Expand Up @@ -90,6 +94,10 @@ blob_open(struct view *view, enum open_flags flags)
else
string_copy_rev(view->ref, view->ops->id);

#if defined HAVE_EDITORCONFIG
state->common.tab_size = editorconfig_tab_size(view->env->file);
#endif

return begin_update(view, NULL, argv, flags);
}

Expand All @@ -104,7 +112,7 @@ blob_read(struct view *view, struct buffer *buf, bool force_stop)
return true;
}

return pager_common_read(view, buf->data, LINE_DEFAULT, NULL);
return pager_common_read(view, buf->data, LINE_DEFAULT, false, NULL);
}

static void
Expand Down
36 changes: 24 additions & 12 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ diff_common_read_diff_wdiff_group(struct diff_stat_context *context)
return true;
}

static bool
static struct line *
diff_common_read_diff_wdiff(struct view *view, const char *text)
{
struct diff_stat_context context = { text, LINE_DEFAULT };
Expand All @@ -282,7 +282,7 @@ diff_common_read_diff_wdiff(struct view *view, const char *text)
return diff_common_add_line(view, text, LINE_DEFAULT, &context);
}

static bool
static struct line *
diff_common_highlight(struct view *view, const char *text, enum line_type type)
{
struct diff_stat_context context = { text, type, true };
Expand All @@ -303,6 +303,7 @@ bool
diff_common_read(struct view *view, const char *data, struct diff_state *state)
{
enum line_type type = get_line_type(data);
struct line *line;

/* ADD2 and DEL2 are only valid in combined diff hunks */
if (!state->combined_diff && (type == LINE_DIFF_ADD2 || type == LINE_DIFF_DEL2))
Expand Down Expand Up @@ -334,7 +335,7 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
}

if (!state->after_commit_title && !prefixcmp(data, " ")) {
struct line *line = add_line_text(view, data, LINE_DEFAULT);
line = add_line_text(view, data, LINE_DEFAULT);

if (line)
line->commit_title = 1;
Expand All @@ -345,13 +346,11 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
if (type == LINE_DIFF_HEADER) {
state->after_diff = true;
state->reading_diff_chunk = false;

} else if (type == LINE_DIFF_CHUNK) {
const int len = chunk_header_marker_length(data);
const char *context = strstr(data + len, "@@");
struct line *line =
context ? add_line_text_at(view, view->lines, data, LINE_DIFF_CHUNK, len)
: NULL;
line = context ? add_line_text_at(view, view->lines, data, LINE_DIFF_CHUNK, len)
: NULL;
struct box *box;

if (!line)
Expand All @@ -363,21 +362,34 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
box->cell[box->cells++].type = LINE_DIFF_STAT;
state->combined_diff = (len > 2);
state->reading_diff_chunk = true;
return true;
goto set_tab_width;

} else if (type == LINE_COMMIT) {
state->reading_diff_chunk = false;

} else if (state->highlight && strchr(data, 0x1b)) {
return diff_common_highlight(view, data, type);

if (!(line = diff_common_highlight(view, data, type)))
return false;
goto set_tab_width;
} else if (opt_word_diff && state->reading_diff_chunk &&
/* combined diff format is not using word diff */
!state->combined_diff) {
return diff_common_read_diff_wdiff(view, data);
if (!(line = diff_common_read_diff_wdiff(view, data)))
return false;
goto set_tab_width;
}

return pager_common_read(view, data, type, NULL);
return pager_common_read(view, data, type, true, &line);

set_tab_width:
#if defined HAVE_EDITORCONFIG
if (type == LINE_DIFF_CHUNK || type == LINE_DEFAULT ||
type == LINE_DIFF_ADD || type == LINE_DIFF_ADD2 ||
type == LINE_DIFF_DEL || type == LINE_DIFF_DEL2) {
line->tab_size = state->common.tab_size;
}
#endif
return true;
}

static bool
Expand Down
6 changes: 0 additions & 6 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,12 +678,6 @@ init_display(void)
keyok(code, false);
}
#endif

#if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
set_tabsize(opt_tab_size);
#else
TABSIZE = opt_tab_size;
#endif
}

static bool
Expand Down
Loading