Skip to content

Commit 3a8bdf6

Browse files
committed
Removed mostly whitespaces when used with "KeepBraceOnTheSameLine".
Still, I recommend using "KeepBraceOnTheSameLine" with the "NormalizeWhitespace" option. Fixed bug where file generates simultaneously.
1 parent 417e521 commit 3a8bdf6

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class CSTOJS : ILog
2828
private Walker? _Walker = null;
2929
private FileSystemWatcher? _FSWatcher = null;
3030

31+
private bool _IsRunning = false;
32+
3133
/// <summary>
3234
/// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
3335
/// </summary>
@@ -272,7 +274,8 @@ private async void OnChanged(object sender, FileSystemEventArgs e)
272274

273275
_Log.WriteLine($"Changed: {e.FullPath}");
274276

275-
await GenerateOneAsync(e.FullPath);
277+
if(!_IsRunning)
278+
await GenerateOneAsync(e.FullPath);
276279
}
277280

278281
private void OnCreated(object sender, FileSystemEventArgs e)
@@ -312,6 +315,8 @@ public void StopWatching()
312315

313316
private void Generate(SyntaxTree tree, Assembly? assembly, List<MetadataReference>? refs = null)
314317
{
318+
_IsRunning = true;
319+
315320
if (_Options.Debug)
316321
{
317322
_Stopwatch.Restart();
@@ -511,9 +516,29 @@ private void Generate(SyntaxTree tree, Assembly? assembly, List<MetadataReferenc
511516

512517
if (_Options.KeepBraceOnTheSameLine)
513518
{
514-
//TODO! remove whitespace trivia before brace!
519+
//Mostly deleted whitespaces, still TODO?
515520
SyntaxToken[] allBraces = trueRoot.DescendantTokens().Where((e) => e.IsKind(SyntaxKind.OpenBraceToken)).ToArray();
521+
522+
List<SyntaxTrivia> allTriviaToDelete = new();
523+
524+
for (int i = 0; i < allBraces.Length; i++)
525+
{
526+
if (allBraces[i].HasLeadingTrivia)
527+
{
528+
SyntaxTriviaList _lt = allBraces[i].LeadingTrivia;
529+
for (int j = 0; j < _lt.Count; j++)
530+
{
531+
allTriviaToDelete.Add(_lt[j]);
532+
}
533+
}
534+
}
535+
//Is this the right way to delete trivia?
536+
trueRoot = trueRoot.ReplaceTrivia(allTriviaToDelete, (o, r) => SyntaxFactory.ElasticMarker);
537+
538+
allBraces = trueRoot.DescendantTokens().Where((e) => e.IsKind(SyntaxKind.OpenBraceToken)).ToArray();
539+
516540
List<SyntaxTrivia> allTriviaToReplace = new();
541+
517542
for (int i = 0; i < allBraces.Length; i++)
518543
{
519544
SyntaxToken _token = allBraces[i].GetPreviousToken();
@@ -527,7 +552,6 @@ private void Generate(SyntaxTree tree, Assembly? assembly, List<MetadataReferenc
527552
}
528553
}
529554
trueRoot = trueRoot.ReplaceTrivia(allTriviaToReplace, (o, r) => SyntaxFactory.Space);
530-
531555
}
532556

533557
if (rtPath != null && rtPath != string.Empty)
@@ -629,6 +653,8 @@ private void Generate(SyntaxTree tree, Assembly? assembly, List<MetadataReferenc
629653
_Stopwatch.Stop();
630654
_Log.WriteLine($"Stop stopwatch: {_Stopwatch.Elapsed}");
631655
}
656+
657+
_IsRunning = false;
632658
}
633659

634660
}

0 commit comments

Comments
 (0)