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

Commit 0a84680

Browse files
Code WET solutions for DRY problems and some code simplifications
1 parent 86a8742 commit 0a84680

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

SourcepawnCondenser/SourcepawnCondenser/CondenserFunctions/SMFunctionConsumer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@ public static List<SMVariable> ConsumeSMVariableLocal(Token[] t, string FileName
379379
continueNext = true;
380380
break;
381381
// Assign var match: "int x = 5"
382-
case TokenKind.Assignment when t[position + 4].Kind == TokenKind.Semicolon &&
383-
(t[position + 3].Kind == TokenKind.Number ||
382+
case TokenKind.Assignment when (t[position + 3].Kind == TokenKind.Number ||
384383
t[position + 3].Kind == TokenKind.Quote ||
385-
t[position + 3].Kind == TokenKind.Identifier):
384+
t[position + 3].Kind == TokenKind.Identifier)
385+
&& t[position + 4].Kind == TokenKind.Semicolon:
386386
variables.Add(new SMVariable
387387
{
388388
Index = startIndex, Length = t[position + 4].Index - startIndex, File = FileName,

SourcepawnCondenser/SourcepawnCondenser/SourcepawnCondenser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<HintPath>..\..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
7575
<Private>True</Private>
7676
</Reference>
77+
<Reference Include="System" />
7778
</ItemGroup>
7879
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7980
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

UI/Components/EditorElement.xaml.cs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public partial class EditorElement : UserControl
6161
private bool SelectionIsHighlited;
6262
private bool WantFoldingUpdate;
6363

64+
private static DateTime lastParsing;
65+
6466
public EditorElement()
6567
{
6668
InitializeComponent();
@@ -522,7 +524,7 @@ public void UpdateFontSize(double size, bool updateLineHeight = true)
522524
public void ToggleCommentOnLine()
523525
{
524526
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
525-
var lineText = editor.Document.GetText(line.Offset, line.Length);
527+
var lineText = editor.Document.GetText(line);
526528
var leadingWhiteSpaces = 0;
527529
foreach (var l in lineText)
528530
if (char.IsWhiteSpace(l))
@@ -547,7 +549,7 @@ public void ToggleCommentOnLine()
547549
private void DuplicateLine(bool down)
548550
{
549551
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
550-
var lineText = editor.Document.GetText(line.Offset, line.Length);
552+
var lineText = editor.Document.GetText(line);
551553
editor.Document.Insert(line.Offset, lineText + Environment.NewLine);
552554
if (down) editor.CaretOffset -= line.Length + 1;
553555
}
@@ -563,7 +565,7 @@ private void MoveLine(bool down)
563565
}
564566
else
565567
{
566-
var lineText = editor.Document.GetText(line.NextLine.Offset, line.NextLine.Length);
568+
var lineText = editor.Document.GetText(line.NextLine);
567569
editor.Document.Remove(line.NextLine.Offset, line.NextLine.TotalLength);
568570
editor.Document.Insert(line.Offset, lineText + Environment.NewLine);
569571
}
@@ -578,7 +580,7 @@ private void MoveLine(bool down)
578580
{
579581
var insertOffset = line.PreviousLine.Offset;
580582
var relativeCaretOffset = editor.CaretOffset - line.Offset;
581-
var lineText = editor.Document.GetText(line.Offset, line.Length);
583+
var lineText = editor.Document.GetText(line);
582584
editor.Document.Remove(line.Offset, line.TotalLength);
583585
editor.Document.Insert(insertOffset, lineText + Environment.NewLine);
584586
editor.CaretOffset = insertOffset + relativeCaretOffset;
@@ -636,9 +638,17 @@ private void Caret_PositionChanged(object sender, EventArgs e)
636638
var result = bracketSearcher.SearchBracket(editor.Document, editor.CaretOffset);
637639
bracketHighlightRenderer.SetHighlight(result);
638640

639-
641+
640642
if (!Program.OptionsObject.Program_DynamicISAC || Program.MainWindow == null) return;
643+
/*
644+
Debug.WriteLineIf(lastParsing != null, $"Diffrence: {DateTime.Now - lastParsing}");
645+
if (lastParsing != null && DateTime.Now - lastParsing < new TimeSpan(0, 0, 2))
646+
return;
647+
648+
lastParsing = DateTime.Now;
641649
650+
Debug.WriteLine("Reparsing");
651+
*/
642652
var ee = Program.MainWindow.GetAllEditorElements();
643653
var ce = Program.MainWindow.GetCurrentEditorElement();
644654

@@ -675,6 +685,7 @@ private void Caret_PositionChanged(object sender, EventArgs e)
675685
var acNodes = smDef.ProduceACNodes();
676686
var isNodes = smDef.ProduceISNodes();
677687

688+
// Lags the hell out when typing a lot.
678689
ce.editor.SyntaxHighlighting = new AeonEditorHighlighting(smDef);
679690
foreach (var el in ee)
680691
{
@@ -726,58 +737,45 @@ private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
726737
// editor.TextArea.IndentationStrategy.IndentLine(editor.Document, editor.Document.GetLineByOffset(editor.CaretOffset));
727738
foldingStrategy.UpdateFoldings(foldingManager, editor.Document);
728739
break;
740+
case "(":
741+
case "[":
729742
case "{":
730-
{
731743
if (Program.OptionsObject.Editor_AutoCloseBrackets)
732744
{
733-
editor.Document.Insert(editor.CaretOffset, "}");
745+
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
746+
var lineText = editor.Document.GetText(line);
747+
748+
// Don't auto close brackets when the user is in a comment or in a string.
749+
if ((lineText[0] == '/' && lineText[1] == '/') ||
750+
editor.Document.GetText(line.Offset, editor.CaretOffset - line.Offset).Count(c => c == '\"') % 2 == 1 ||
751+
editor.Document.GetText(line.Offset - 3, 1) == "\\")
752+
break;
753+
754+
// Getting the char ascii code with int cast and the string pos 0 (the char it self),
755+
// if it's a ( i need to add 1 to get the ascii code for closing bracket
756+
// for [ and { i need to add 2 to get the closing bracket ascii code
757+
char closingBracket = (char)((int)e.Text[0] + (e.Text == "(" ? 1 : 2));
758+
editor.Document.Insert(editor.CaretOffset, closingBracket.ToString());
734759
editor.CaretOffset -= 1;
735-
}
736760

737-
foldingStrategy.UpdateFoldings(foldingManager, editor.Document);
738-
break;
739-
}
740-
default:
741-
{
742-
if (Program.OptionsObject.Editor_AutoCloseBrackets)
743-
{
744-
if (e.Text == "(")
745-
{
746-
editor.Document.Insert(editor.CaretOffset, ")");
747-
editor.CaretOffset -= 1;
748-
}
749-
else if (e.Text == "[")
750-
{
751-
editor.Document.Insert(editor.CaretOffset, "]");
752-
editor.CaretOffset -= 1;
753-
}
761+
// If it's a code block bracket we need to update the folding
762+
if (e.Text == "{")
763+
foldingStrategy.UpdateFoldings(foldingManager, editor.Document);
754764
}
755765

756766
break;
757-
}
758767
}
759768

760769
if (Program.OptionsObject.Editor_AutoCloseStringChars)
761770
{
762-
if (e.Text == "\"")
763-
{
764-
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
765-
var lineText = editor.Document.GetText(line.Offset, editor.CaretOffset - line.Offset);
766-
if (lineText.Length > 0)
767-
if (lineText[Math.Max(lineText.Length - 2, 0)] != '\\')
768-
{
769-
editor.Document.Insert(editor.CaretOffset, "\"");
770-
editor.CaretOffset -= 1;
771-
}
772-
}
773-
else if (e.Text == "'")
771+
if (e.Text == "\"" || e.Text == "'")
774772
{
775773
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
776774
var lineText = editor.Document.GetText(line.Offset, editor.CaretOffset - line.Offset);
777775
if (lineText.Length > 0)
778776
if (lineText[Math.Max(lineText.Length - 2, 0)] != '\\')
779777
{
780-
editor.Document.Insert(editor.CaretOffset, "'");
778+
editor.Document.Insert(editor.CaretOffset, e.Text);
781779
editor.CaretOffset -= 1;
782780
}
783781
}

UI/MainWindowCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private void Command_TidyCode(bool All)
224224
// 0 - start | any other - middle | -1 - EOS
225225
int curserLinePos = currentCaret == line.Offset ? 0 : currentCaret == line.EndOffset ? -1 : currentCaret - line.Offset;
226226

227-
if (curserLinePos > 0) numOfSpacesOrTabsBefore = ee.editor.Document.GetText(line.Offset, curserLinePos).Count(c => c == ' ' || c == '\t');
227+
if (curserLinePos > 0) numOfSpacesOrTabsBefore = ee.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
228228

229229
#if DEBUG
230230
Debug.WriteLine($"Curser offset before format: {currentCaret}");
@@ -252,7 +252,7 @@ private void Command_TidyCode(bool All)
252252
}
253253
else if(curserLinePos != 0)
254254
{
255-
int numOfSpacesOrTabsAfter = ee.editor.Document.GetText(line.Offset, curserLinePos).Count(c => c == ' ' || c == '\t');
255+
int numOfSpacesOrTabsAfter = ee.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
256256
newCaretPos += curserLinePos + (numOfSpacesOrTabsAfter - numOfSpacesOrTabsBefore);
257257
#if DEBUG
258258
Debug.WriteLine($"Curser offset after format: {newCaretPos}");

0 commit comments

Comments
 (0)