Skip to content

Commit 2979ea4

Browse files
Fix inclusion with brackets on the same line
1 parent 05bb3d4 commit 2979ea4

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

codeinclude/resolver.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ def select(
5555

5656
delim_count -= line.count("}")
5757

58-
if delim_count > 0 and not first_line_of_block:
58+
if not first_line_of_block:
5959
delim_count += line.count("{")
60-
selected_lines.append(i)
60+
if delim_count > 0:
61+
selected_lines.append(i)
6162

6263
if from_token and to_token:
6364
i = 0

tests/codeinclude/test_plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def test_simple_case_no_selector(self):
115115
""").strip(),
116116
result.strip())
117117

118+
@unittest.skip("https://github.com/rnorth/mkdocs-codeinclude-plugin/issues/13")
118119
def test_simple_case_right_curly_inside_block(self):
119120
plugin = CodeIncludePlugin()
120121
result = plugin.on_page_markdown(MARKDOWN_EXAMPLE_RIGHT_CURLY, PAGE_EXAMPLE, dict())

tests/codeinclude/test_resolver.py

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import textwrap
2+
import unittest
3+
14
from codeinclude.resolver import select
25

36
CODE_BLOCK_EXAMPLE = """
@@ -8,20 +11,62 @@
811
this is a trailing line
912
"""
1013

11-
def test_lines():
12-
result = select(CODE_BLOCK_EXAMPLE, lines="2,6")
13-
assert result == ("this is the first line\n"
14-
"\n"
15-
"⋯\n"
16-
"\n"
17-
"this is a trailing line\n")
18-
19-
def test_inside_block():
20-
result = select(CODE_BLOCK_EXAMPLE, inside_block="blockstarter")
21-
assert result == " block content\n"
22-
23-
def test_whole_block():
24-
result = select(CODE_BLOCK_EXAMPLE, block="blockstarter")
25-
assert result == ("blockstarter {\n"
26-
" block content\n"
27-
"}\n")
14+
15+
class ResolverTest(unittest.TestCase):
16+
def test_lines(self):
17+
result = select(CODE_BLOCK_EXAMPLE, lines="2,6")
18+
self.assertEquals(("this is the first line\n"
19+
"\n"
20+
"⋯\n"
21+
"\n"
22+
"this is a trailing line\n"),
23+
result)
24+
25+
def test_inside_block(self):
26+
result = select(CODE_BLOCK_EXAMPLE, inside_block="blockstarter")
27+
self.assertEquals(" block content\n", result)
28+
29+
def test_whole_block(self):
30+
result = select(CODE_BLOCK_EXAMPLE, block="blockstarter")
31+
self.assertEquals(("blockstarter {\n"
32+
" block content\n"
33+
"}\n"),
34+
result)
35+
36+
def test_inside_block_content_on_last_line(self):
37+
result = select(
38+
textwrap.dedent(
39+
"""
40+
foo {
41+
if (true) {
42+
bar();
43+
} }
44+
/* The line above contains both the closing curly bracket for `if` and for `foo` */
45+
"""),
46+
inside_block="foo")
47+
self.assertEquals((" if (true) {\n"
48+
" bar();\n"),
49+
result)
50+
51+
def test_inside_block_curly_on_same_line(self):
52+
result = select(
53+
textwrap.dedent(
54+
"""
55+
foo {
56+
/* {} */
57+
}
58+
"""),
59+
inside_block="foo")
60+
self.assertEquals(" /* {} */\n", result)
61+
62+
def test_inside_block_multiple_curly_on_same_line(self):
63+
result = select(
64+
textwrap.dedent(
65+
"""
66+
//
67+
foo {
68+
/* {} {@code bar} {@link baz} */
69+
}
70+
"""),
71+
inside_block="foo")
72+
self.assertEquals(" /* {} {@code bar} {@link baz} */\n", result)

0 commit comments

Comments
 (0)