Skip to content

Commit

Permalink
Update RefactorWindow.axaml.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
FixeQyt authored Aug 1, 2024
1 parent f178f76 commit c02fca9
Showing 1 changed file with 72 additions and 37 deletions.
109 changes: 72 additions & 37 deletions SkEditor/Views/RefactorWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
namespace SkEditor.Views;
public partial class RefactorWindow : AppWindow
{
private TextEditor _textEditor;

public RefactorWindow()
{
InitializeComponent();

_textEditor = SkEditorAPI.Files.GetCurrentOpenedFile().Editor;
ApplyButton.Command = new RelayCommand(Apply);
}

Expand All @@ -23,54 +24,59 @@ private async void Apply()
if (RemoveCommentsCheckBox.IsChecked == true) await RemoveComments();
if (TabsToSpacesCheckBox.IsChecked == true) await TabsToSpaces();
if (SpacesToTabsCheckBox.IsChecked == true) await SpacesToTabs();

Close();
}
private static async Task RemoveComments()

private async Task RemoveComments()
{
TextEditor textEditor = SkEditorAPI.Files.GetCurrentOpenedFile().Editor;
string text = textEditor.Document.Text;
string text = _textEditor.Document.Text;

text = RemoveMultiLineComments(text);

text = RemoveSingleLineComments(text);

textEditor.Document.Text = text;

_textEditor.Document.Text = text;
await Task.CompletedTask;
}

private static string RemoveMultiLineComments(string text)
{
var commentChar = "###";
int startIndex = 0;
int index = 0;
bool insideQuotes = false;

while ((startIndex = text.IndexOf(commentChar, startIndex)) != -1)
while ((index = text.IndexOf(commentChar, index)) != -1)
{
int endIndex = text.IndexOf(commentChar, startIndex + commentChar.Length);
if (endIndex == -1) break;
if (IsInsideQuotes(text, startIndex))
if (IsInsideQuotes(text, index, out insideQuotes))
{
startIndex = endIndex + commentChar.Length;
index += commentChar.Length;
continue;
}

int endIndex = text.IndexOf(commentChar, index + commentChar.Length);
if (endIndex == -1) break;

endIndex += commentChar.Length;
text = text.Remove(startIndex, endIndex - startIndex);
text = text.Remove(index, endIndex - index);
}

return text;
}

private static bool IsInsideQuotes(string text, int index)
private static bool IsInsideQuotes(string text, int index, out bool insideQuotes)
{
int quoteCount = 0;
insideQuotes = false;

for (int i = 0; i < index; i++)
{
if (text[i] == '"') quoteCount++;
if (text[i] == '"')
{
quoteCount++;
}
}
return (quoteCount % 2 != 0);

insideQuotes = (quoteCount % 2 != 0);
return insideQuotes;
}

private static string RemoveSingleLineComments(string text)
Expand All @@ -80,7 +86,7 @@ private static string RemoveSingleLineComments(string text)

foreach (var line in lines)
{
var commentIndex = line.IndexOf('#');
int commentIndex = GetCommentIndex(line);
if (commentIndex != -1)
{
builder.AppendLine(line.Substring(0, commentIndex).TrimEnd());
Expand All @@ -94,52 +100,81 @@ private static string RemoveSingleLineComments(string text)
return builder.ToString();
}

private static Task TabsToSpaces()
private static int GetCommentIndex(string line)
{
int index = line.IndexOf('#');
while (index != -1)
{
if (IsInsideQuotes(line, index))
{
index = line.IndexOf('#', index + 1);
}
else
{
return index;
}
}
return -1;
}

private static bool IsInsideQuotes(string text, int index)
{
TextEditor textEditor = SkEditorAPI.Files.GetCurrentOpenedFile().Editor;
var lines = textEditor.Document.Lines;
int quoteCount = 0;

for (int i = 0; i < index; i++)
{
if (text[i] == '"')
{
quoteCount++;
}
}

return (quoteCount % 2 != 0);
}

private async Task TabsToSpaces()
{
var lines = _textEditor.Document.Lines;

lines.Where(line => GetText(line).StartsWith("\t")).ToList().ForEach(line =>
{
int tabs = GetText(line).TakeWhile(x => x == '\t').Count();
textEditor.Document.Replace(line.Offset, tabs, new string(' ', tabs * 4));
_textEditor.Document.Replace(line.Offset, tabs, new string(' ', tabs * 4));
});
return Task.CompletedTask;

await Task.CompletedTask;
}

private static Task SpacesToTabs()
private async Task SpacesToTabs()
{
TextEditor textEditor = SkEditorAPI.Files.GetCurrentOpenedFile().Editor;
int tabSize = GetTabSize();
var lines = textEditor.Document.Lines;
var lines = _textEditor.Document.Lines;

lines.Where(line => GetText(line).StartsWith(" ")).ToList().ForEach(line =>
{
int spaces = GetText(line).TakeWhile(x => x == ' ').Count();
if (spaces % tabSize == 0)
{
textEditor.Document.Replace(line.Offset, spaces, new string('\t', spaces / tabSize));
_textEditor.Document.Replace(line.Offset, spaces, new string('\t', spaces / tabSize));
}
});

return Task.CompletedTask;
await Task.CompletedTask;
}

private static int GetTabSize()
private int GetTabSize()
{
TextEditor textEditor = SkEditorAPI.Files.GetCurrentOpenedFile().Editor;
var lines = textEditor.Document.Lines;
var lines = _textEditor.Document.Lines;
int tabSize = 4;

var line = lines.FirstOrDefault(line => GetText(line).StartsWith(" "));

if (line != null) tabSize = GetText(line).TakeWhile(x => x == ' ').Count();

return tabSize;
}

private static string GetText(DocumentLine line)
private string GetText(DocumentLine line)
{
TextEditor textEditor = SkEditorAPI.Files.GetCurrentOpenedFile().Editor;
return textEditor.Document.GetText(line.Offset, line.Length);
return _textEditor.Document.GetText(line.Offset, line.Length);
}
}

0 comments on commit c02fca9

Please sign in to comment.