Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 75a6257

Browse files
committed
fixed reformat broken with escaped backslashes between quotes
1 parent 51397f1 commit 75a6257

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ csharp_new_line_between_query_expression_clauses = true
131131
csharp_indent_block_contents = true
132132
csharp_indent_braces = false
133133
csharp_indent_case_contents = true
134-
csharp_indent_case_contents_when_block = true
134+
csharp_indent_case_contents_when_block =false
135135
csharp_indent_labels = one_less_than_current
136136
csharp_indent_switch_labels = true
137137

UI/MainWindow/MainWindowCommands.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,9 @@ private void Command_TidyCode(bool All)
569569
var line = ee.editor.Document.GetLineByOffset(currentCaret);
570570
var lineNumber = line.LineNumber;
571571
// 0 - start | any other - middle | -1 - EOS
572-
var curserLinePos = currentCaret == line.Offset ? 0 : currentCaret == line.EndOffset ? -1 : currentCaret - line.Offset;
572+
var cursorLinePos = currentCaret == line.Offset ? 0 : currentCaret == line.EndOffset ? -1 : currentCaret - line.Offset;
573573

574-
if (curserLinePos > 0)
574+
if (cursorLinePos > 0)
575575
{
576576
numOfSpacesOrTabsBefore = ee.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
577577
}
@@ -585,14 +585,14 @@ private void Command_TidyCode(bool All)
585585

586586
line = ee.editor.Document.GetLineByNumber(lineNumber);
587587
var newCaretPos = line.Offset;
588-
if (curserLinePos == -1)
588+
if (cursorLinePos == -1)
589589
{
590590
newCaretPos += line.Length;
591591
}
592-
else if (curserLinePos != 0)
592+
else if (cursorLinePos != 0)
593593
{
594594
var numOfSpacesOrTabsAfter = ee.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
595-
newCaretPos += curserLinePos + (numOfSpacesOrTabsAfter - numOfSpacesOrTabsBefore);
595+
newCaretPos += cursorLinePos + (numOfSpacesOrTabsAfter - numOfSpacesOrTabsBefore);
596596
}
597597
ee.editor.TextArea.Caret.Offset = newCaretPos;
598598
}

Utils/SPSyntaxTidy/SPTokenizer.cs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ public static SPToken[] Tokenize(string source)
1818

1919
#region Newline
2020

21-
if (c == '\n'
22-
) //just fetch \n. \r will be killed by the whitestrip but it's reintroduced in Environment.NewLine
21+
//just fetch \n. \r will be killed by the whitestrip but it's reintroduced in Environment.NewLine
22+
if (c == '\n')
2323
{
24+
//add them before the whitestrip-killer will get them ^^
2425
token.Add(new SPToken()
2526
{
2627
Kind = SPTokenKind.Newline,
2728
Value = Environment.NewLine
28-
}); //add them before the whitestrip-killer will get them ^^
29+
});
2930
continue;
3031
}
3132

@@ -42,17 +43,49 @@ public static SPToken[] Tokenize(string source)
4243

4344
#region Quotes
4445

45-
if (c == '"') //sigh...
46+
if (c == '"')
4647
{
4748
var startIndex = i;
48-
var
49-
foundOccurence =
50-
false; //these suckers are here because we want to continue the main-for-loop but cannot do it from the for-loop in the nextline
49+
var foundOccurence = false;
50+
51+
// keep searching for next quote
5152
for (var j = i + 1; j < length; ++j)
5253
{
54+
// if found, search for an escape slash before it
5355
if (buffer[j] == '"')
5456
{
55-
if (buffer[j - 1] != '\\') //is the quote not escaped?
57+
if (buffer[j - 1] == '\\')
58+
{
59+
// if found, count the amount of them
60+
var slashAmount = 0;
61+
for (int k = j - 1; k >= 0; k--)
62+
{
63+
if (buffer[k - 1] == '\\')
64+
{
65+
slashAmount++;
66+
continue;
67+
}
68+
break;
69+
}
70+
// if amount is even (slashAmout + 1 already counted = it's even)
71+
// quote is not escaped and counts as closing quote, we add it as token
72+
if (slashAmount % 2 != 0)
73+
{
74+
token.Add(new SPToken()
75+
{
76+
Kind = SPTokenKind.Quote,
77+
Value = source.Substring(startIndex, j - startIndex + 1)
78+
});
79+
foundOccurence = true;
80+
i = j; //skip it in the main loop
81+
break;
82+
}
83+
else
84+
{
85+
continue;
86+
}
87+
}
88+
else
5689
{
5790
token.Add(new SPToken()
5891
{
@@ -124,8 +157,8 @@ public static SPToken[] Tokenize(string source)
124157
++i;
125158
for (var j = i; j < length; ++j)
126159
{
127-
if (buffer[j] == '\r' || buffer[j] == '\n'
128-
) //different line ending specifications...horribly...
160+
//different line ending specifications...horribly...
161+
if (buffer[j] == '\r' || buffer[j] == '\n')
129162
{
130163
break;
131164
}
@@ -259,8 +292,7 @@ public static SPToken[] Tokenize(string source)
259292
}
260293
}
261294

262-
if (c == '<' || c == '>' || c == '!' || c == '|' || c == '&' || c == '+' || c == '-' || c == '*' ||
263-
c == '/' || c == '^' || c == '%')
295+
if (c is '<' or '>' or '!' or '|' or '&' or '+' or '-' or '*' or '/' or '^' or '%')
264296
{
265297
if ((i + 1) < length)
266298
{
@@ -272,8 +304,8 @@ public static SPToken[] Tokenize(string source)
272304
}
273305
}
274306

275-
if (c != '!' && c != '|' && c != '&' && c != '+' && c != '-' && c != '<' && c != '>'
276-
) //they can have another meaning so they are handled on their own
307+
//they can have another meaning so they are handled on their own
308+
if (c is not '!' and not '|' and not '&' and not '+' and not '-' and not '<' and not '>')
277309
{
278310
token.Add(new SPToken() { Kind = SPTokenKind.Operator, Value = source.Substring(i, 1) });
279311
continue;
@@ -342,8 +374,8 @@ public static SPToken[] Tokenize(string source)
342374
}
343375
}
344376

345-
if (c == '&'
346-
) //the & operator is a little bit problematic. It can mean bitwise AND or address of variable. This is not easy to determinate
377+
//the & operator is a little bit problematic. It can mean bitwise AND or address of variable. This is not easy to determinate
378+
if (c == '&')
347379
{
348380
var canMatchSingle = true;
349381
if ((i + 1) < length)

0 commit comments

Comments
 (0)