Skip to content

Commit

Permalink
Remove support for inline comments in .editorconfig files
Browse files Browse the repository at this point in the history
The EditorConfig specification now explicitly states that comments
can only start at the beginning of a line and that "inline comments"
are no longer recognized as such.

See also:

* editorconfig/specification#31
* https://github.com/editorconfig/specification/pull/31/files#diff-54a294a5d016e1a8e98bc95668ed84a99a9edd5c10394d9a2b1ee848006e98a7R91
  • Loading branch information
craigbarnes committed May 31, 2023
1 parent 375c894 commit a61b90f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
26 changes: 1 addition & 25 deletions src/editorconfig/ini.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
#include "ini.h"
#include "util/ascii.h"
#include "util/debug.h"
#include "util/str-util.h"

static void strip_trailing_comments_and_whitespace(StringView *line)
{
const char *str = line->data;
size_t len = line->length;

// Remove inline comments
char prev_char = '\0';
for (size_t i = len; i > 0; i--) {
if (ascii_isspace(str[i]) && (prev_char == '#' || prev_char == ';')) {
len = i;
}
prev_char = str[i];
}

// Trim trailing whitespace
const char *ptr = str + len - 1;
while (ptr > str && ascii_isspace(*ptr--)) {
len--;
}

line->length = len;
}

bool ini_parse(IniParser *ctx)
{
const char *input = ctx->input;
Expand All @@ -39,7 +15,7 @@ bool ini_parse(IniParser *ctx)
continue;
}

strip_trailing_comments_and_whitespace(&line);
strview_trim_right(&line);
BUG_ON(line.length == 0);
if (line.data[0] == '[') {
if (strview_has_suffix(&line, "]")) {
Expand Down
4 changes: 2 additions & 2 deletions test/data/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

root = true

[*.?{foo,bar}.[xyz]] # inline comment
indent_style = space # another inline comment
[*.?{foo,bar}.[xyz]]
indent_style = space
indent_size = 3
max_line_length = 68
= property with no name
Expand Down
12 changes: 6 additions & 6 deletions test/editorconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
static void test_ini_parse(TestContext *ctx)
{
static const char input[] =
" \t key = val # inline comment \n"
" \t key = val \n"
"\n"
" \t [section 1] #; ...\n"
" \t [section 1] \n"
"xyz = 123\n"
"\tfoo bar = this;is#not#a;comment\n"
"[section 2]\n"
Expand All @@ -26,7 +26,7 @@ static void test_ini_parse(TestContext *ctx)
ASSERT_NONNULL(ini.name.data);
ASSERT_NONNULL(ini.value.data);
EXPECT_NULL(ini.section.data);
EXPECT_EQ(ini.pos, 39);
EXPECT_EQ(ini.pos, 17);
EXPECT_EQ(ini.name_count, 1);
EXPECT_TRUE(strview_equal_cstring(&ini.name, "key"));
EXPECT_TRUE(strview_equal_cstring(&ini.value, "val"));
Expand All @@ -35,7 +35,7 @@ static void test_ini_parse(TestContext *ctx)
ASSERT_NONNULL(ini.name.data);
ASSERT_NONNULL(ini.value.data);
ASSERT_NONNULL(ini.section.data);
EXPECT_EQ(ini.pos, 72);
EXPECT_EQ(ini.pos, 45);
EXPECT_EQ(ini.name_count, 1);
EXPECT_TRUE(strview_equal_cstring(&ini.section, "section 1"));
EXPECT_TRUE(strview_equal_cstring(&ini.name, "xyz"));
Expand All @@ -45,7 +45,7 @@ static void test_ini_parse(TestContext *ctx)
ASSERT_NONNULL(ini.name.data);
ASSERT_NONNULL(ini.value.data);
ASSERT_NONNULL(ini.section.data);
EXPECT_EQ(ini.pos, 105);
EXPECT_EQ(ini.pos, 78);
EXPECT_EQ(ini.name_count, 2);
EXPECT_TRUE(strview_equal_cstring(&ini.section, "section 1"));
EXPECT_TRUE(strview_equal_cstring(&ini.name, "foo bar"));
Expand All @@ -55,7 +55,7 @@ static void test_ini_parse(TestContext *ctx)
ASSERT_NONNULL(ini.name.data);
ASSERT_NONNULL(ini.value.data);
ASSERT_NONNULL(ini.section.data);
EXPECT_EQ(ini.pos, 121);
EXPECT_EQ(ini.pos, 94);
EXPECT_EQ(ini.name_count, 1);
EXPECT_TRUE(strview_equal_cstring(&ini.section, "section 2"));
EXPECT_TRUE(strview_equal_cstring(&ini.name, "x"));
Expand Down

0 comments on commit a61b90f

Please sign in to comment.