Skip to content

Commit

Permalink
feat: add snippet blocks to autocomplete menu
Browse files Browse the repository at this point in the history
  • Loading branch information
PocketMiner82 committed Apr 12, 2024
1 parent 94a3b5b commit 42cd6a8
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 130 deletions.
8 changes: 0 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,6 @@ dotnet_naming_rule.constant_should_be_capslock.severity = warning
dotnet_naming_rule.constant_should_be_capslock.symbols = constant
dotnet_naming_rule.constant_should_be_capslock.style = capslock

dotnet_naming_rule.readonly_should_be_capslock.severity = warning
dotnet_naming_rule.readonly_should_be_capslock.symbols = readonly
dotnet_naming_rule.readonly_should_be_capslock.style = capslock

dotnet_naming_rule.private_or_internal_field_should_be_camelcase_with_underscore_prefix.severity = warning
dotnet_naming_rule.private_or_internal_field_should_be_camelcase_with_underscore_prefix.symbols = private_or_internal_field
dotnet_naming_rule.private_or_internal_field_should_be_camelcase_with_underscore_prefix.style = camelcase_with_underscore_prefix
Expand Down Expand Up @@ -245,10 +241,6 @@ dotnet_naming_symbols.constant.applicable_kinds = namespace, property, field, ev
dotnet_naming_symbols.constant.applicable_accessibilities = *
dotnet_naming_symbols.constant.required_modifiers = const

dotnet_naming_symbols.readonly.applicable_kinds = namespace, property, field, event, parameter, local, class, struct, interface, enum, delegate, method, local_function
dotnet_naming_symbols.readonly.applicable_accessibilities = *
dotnet_naming_symbols.readonly.required_modifiers = readonly

# Benennungsstile

dotnet_naming_style.pascal_case.required_prefix =
Expand Down
2 changes: 1 addition & 1 deletion pseudocodeIde/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY

using pseudocodeIde.interpreter;
using pseudocode_ide.interpreter.pseudocode;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down
4 changes: 3 additions & 1 deletion pseudocodeIde/PseudocodeIDEForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 39 additions & 38 deletions pseudocodeIde/PseudocodeIDEForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.VisualBasic.FileIO;
using Newtonsoft.Json;
using pseudocode_ide;
using pseudocode_ide.interpreter.pseudocode;
using pseudocode_ide.interpreter.scanner;
using pseudocodeIde.interpreter;
using ScintillaNET;
Expand All @@ -39,7 +40,7 @@ public partial class PseudocodeIDEForm : Form
/// <summary>
/// do not update the undo stack when the last char is a letter from A-Z (case ignored), a number from 0-9 or a underscore
/// </summary>
private readonly Regex NO_UPDATE_AFTER = new Regex(@"^[a-zA-Z0-9_äöüÄÖÜß]$", RegexOptions.Multiline);
private readonly Regex _noUpdateAfter = new Regex(@"^[a-zA-Z0-9_äöüÄÖÜß]$", RegexOptions.Multiline);

/// <summary>
/// the code currently in the textbox
Expand Down Expand Up @@ -72,7 +73,7 @@ public string Code

// undo and redo stacks
private Stack<string> _undoStack = new Stack<string>();
private readonly Stack<string> REDO_STACK = new Stack<string>();
private readonly Stack<string> _redoStack = new Stack<string>();

/// <summary>
/// the path where this file is saved
Expand All @@ -87,12 +88,12 @@ public string Code
/// <summary>
/// The find and replace forms for the code textbox.
/// </summary>
private readonly FindReplace FIND_REPLACE;
private readonly FindReplace _findReplace;

/// <summary>
/// The output form where the pseudocode execution logs are displayed.
/// </summary>
private readonly OutputForm OUTPUT_FORM;
private readonly OutputForm _outputForm;

/// <summary>
/// The maximum length of the line number characters.
Expand All @@ -103,9 +104,9 @@ public string Code
public PseudocodeIDEForm()
{
InitializeComponent();
OUTPUT_FORM = new OutputForm(this);
FIND_REPLACE = new FindReplace(codeTextBox);
FIND_REPLACE.KeyPressed += CodeTextBox_KeyDown;
_outputForm = new OutputForm(this);
_findReplace = new FindReplace(codeTextBox);
_findReplace.KeyPressed += CodeTextBox_KeyDown;

ResetUndoRedo();

Expand All @@ -126,9 +127,9 @@ private void BuildAutocompleteMenu()

List<AutocompleteItem> items = new List<AutocompleteItem>();

foreach (string item in Scanner.KEYWORDS.Keys)
foreach (List<PseudocodeAutocompleteItem> keywordItems in PseudocodeKeywords.KEYWORDS.Values)
{
items.Add(new SnippetAutocompleteItem(item) { ImageIndex = 1 });
items.AddRange(keywordItems);
}

autoCompleteMenu.SetAutocompleteItems(items);
Expand Down Expand Up @@ -163,21 +164,21 @@ private void ConfigureCodeTextBox()
codeTextBox.Styles[Style.Default].Font = "Courier New";
codeTextBox.StyleClearAll();

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_DEFAULT].ForeColor = Color.DarkGray;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_DEFAULT].ForeColor = Color.DarkGray;

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_KEYWORD].ForeColor = Color.Blue;
codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_KEYWORD].Bold = true;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_KEYWORD].ForeColor = Color.Blue;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_KEYWORD].Bold = true;

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_IDENTIFIER].ForeColor = Color.Black;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_IDENTIFIER].ForeColor = Color.Black;

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_NUMBER].ForeColor = Color.Peru;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_NUMBER].ForeColor = Color.Peru;

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_STRING].ForeColor = Color.OrangeRed;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_STRING].ForeColor = Color.OrangeRed;

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_ESCAPE].ForeColor = Color.Orange;
codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_ESCAPE].Bold = true;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_ESCAPE].ForeColor = Color.Orange;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_ESCAPE].Bold = true;

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_COMMENT].ForeColor = Color.Green;
codeTextBox.Styles[SyntaxHighlightingScanner.STYLE_COMMENT].ForeColor = Color.Green;

codeTextBox.LexerName = "";
codeTextBox.StyleNeeded += CodeTextBox_StyleNeeded;
Expand All @@ -188,7 +189,7 @@ private void CodeTextBox_StyleNeeded(object sender, StyleNeededEventArgs e)
int startPos = codeTextBox.GetEndStyled();
int endPos = e.Position;

SyntaxHighlightingLexer.Style(codeTextBox, startPos, endPos);
SyntaxHighlightingScanner.Style(codeTextBox, startPos, endPos);
}

private void WordWrapMenuItem_Click(object sender, EventArgs e)
Expand All @@ -206,9 +207,9 @@ private void SingleEqualIsCompareOperatorMenuItem_Click(object sender, EventArgs
private void PseudocodeIDE_FormClosing(object sender, FormClosingEventArgs e)
{
// main form won't close if child form is not disposed
if (!OUTPUT_FORM.IsDisposed)
if (!_outputForm.IsDisposed)
{
OUTPUT_FORM.Close();
_outputForm.Close();
Close();
return;
}
Expand Down Expand Up @@ -297,12 +298,12 @@ private void CodeTextBox_KeyDown(object sender, KeyEventArgs e)
}
else if (e.Shift && e.KeyCode == Keys.F3)
{
FIND_REPLACE.Window.FindPrevious();
_findReplace.Window.FindPrevious();
e.SuppressKeyPress = true;
}
else if (e.KeyCode == Keys.F3)
{
FIND_REPLACE.Window.FindNext();
_findReplace.Window.FindNext();
e.SuppressKeyPress = true;
}
}
Expand Down Expand Up @@ -418,7 +419,7 @@ private void CodeTextBox_CharAdded(object sender, CharAddedEventArgs e)
private void UserChangedSelection()
{
// ignore when we already undid something
if (REDO_STACK.Count != 0)
if (_redoStack.Count != 0)
{
_lastCursorPosition = codeTextBox.SelectionStart;
return;
Expand Down Expand Up @@ -666,7 +667,7 @@ private void OpenFile()
private void ResetUndoRedo()
{
_undoStack.Clear();
REDO_STACK.Clear();
_redoStack.Clear();
_undoStack.Push(codeTextBox.Text);
undoToolStripMenuItem.Enabled = false;
redoToolStripMenuItem.Enabled = false;
Expand All @@ -686,9 +687,9 @@ public void UpdateUndoStack(bool forceUpdate)

// when user writes something new, the redo stack will be cleared
undoToolStripMenuItem.Enabled = true;
if (REDO_STACK.Count != 0)
if (_redoStack.Count != 0)
{
REDO_STACK.Clear();
_redoStack.Clear();
redoToolStripMenuItem.Enabled = false;
}

Expand All @@ -710,14 +711,14 @@ public void UpdateUndoStack(bool forceUpdate)

// don't update when the last char matches the NO_UPDATE_AFTER regex and the update is caused by text change.
// also don't update, when the new undo text is already in the stack
if ((codeTextBox.SelectionStart > 1 && !forceUpdate && NO_UPDATE_AFTER.IsMatch(undoText.ElementAt(codeTextBox.SelectionStart - 2).ToString()))
if ((codeTextBox.SelectionStart > 1 && !forceUpdate && _noUpdateAfter.IsMatch(undoText.ElementAt(codeTextBox.SelectionStart - 2).ToString()))
|| _undoStack.Peek().Equals(undoText))
{
return;
}

// again, make sure that the redo stack is empty
REDO_STACK.Clear();
_redoStack.Clear();

// and update the undo stack, limit max undo size
_undoStack.Push(undoText);
Expand All @@ -727,9 +728,9 @@ public void UpdateUndoStack(bool forceUpdate)
private void UndoToolStripMenuItem_Click(object sender, EventArgs e)
{
// put current text in redo if not already in there
if (REDO_STACK.Count == 0 || !REDO_STACK.Peek().Equals(codeTextBox.Text))
if (_redoStack.Count == 0 || !_redoStack.Peek().Equals(codeTextBox.Text))
{
REDO_STACK.Push(codeTextBox.Text);
_redoStack.Push(codeTextBox.Text);
redoToolStripMenuItem.Enabled = true;
}

Expand Down Expand Up @@ -771,16 +772,16 @@ private void RedoToolStripMenuItem_Click(object sender, EventArgs e)
}

// set the textbox text to the top item of the redo stack. also remove it from the stack
if (REDO_STACK.Count > 0)
if (_redoStack.Count > 0)
{
IgnoreTextChange = true;
int oldSelectionStart = codeTextBox.SelectionStart;
codeTextBox.Text = REDO_STACK.Pop();
codeTextBox.Text = _redoStack.Pop();
codeTextBox.SelectionStart = Math.Min(oldSelectionStart, codeTextBox.TextLength);
}

// no more things to redo
if (REDO_STACK.Count == 0)
if (_redoStack.Count == 0)
{
redoToolStripMenuItem.Enabled = false;
}
Expand All @@ -792,12 +793,12 @@ private void RedoToolStripMenuItem_Click(object sender, EventArgs e)

private void FindMenuItem_Click(object sender, EventArgs e)
{
FIND_REPLACE.ShowFind();
_findReplace.ShowFind();
}

private void ReplaceMenuItem_Click(object sender, EventArgs e)
{
FIND_REPLACE.ShowReplace();
_findReplace.ShowReplace();
}

private void GoToMenuItem_Click(object sender, EventArgs e)
Expand All @@ -811,12 +812,12 @@ private void GoToMenuItem_Click(object sender, EventArgs e)

private void RunProgramMenuItem_Click(object sender, EventArgs e)
{
OUTPUT_FORM.ShowAndRun();
_outputForm.ShowAndRun();
}

private void OpenOutputFormMenuItem_Click(object sender, EventArgs e)
{
OUTPUT_FORM.Show();
_outputForm.Show();
}

// ---------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion pseudocodeIde/interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY

using pseudocode_ide.interpreter.csharpcode;
using pseudocode_ide.interpreter.pseudocode;
using pseudocodeIde.interpreter.logging;
using pseudocodeIde.interpreter.parser;
using System;
using System.Collections.Generic;
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using pseudocodeIde;
using pseudocodeIde.interpreter;
using pseudocodeIde.interpreter.logging;
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Threading;

namespace pseudocodeIde.interpreter.parser
namespace pseudocode_ide.interpreter.csharpcode
{
public class CSharpCodeManager
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// THIS FILE IS NOT COMPILED AS C# CODE.
// IT IS AN EMBEDDED RESSOURCE.
//
Expand Down
5 changes: 3 additions & 2 deletions pseudocodeIde/interpreter/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY

using pseudocode_ide.interpreter.csharpcode;
using pseudocode_ide.interpreter.pseudocode;
using pseudocodeIde.interpreter.logging;
using pseudocodeIde.interpreter.parser;
using System.Collections.Generic;
using System.Linq;
using static pseudocodeIde.interpreter.TokenType;
using static pseudocode_ide.interpreter.pseudocode.TokenType;

namespace pseudocodeIde.interpreter
{
Expand Down
50 changes: 50 additions & 0 deletions pseudocodeIde/interpreter/pseudocode/PseudocodeAutocompleteItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Pseudocode IDE - Execute Pseudocode for the German (BW) 2024 Abitur
// Copyright (C) 2024 PocketMiner82
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY

using AutocompleteMenuNS;
using pseudocodeIde;
using ScintillaNET;

namespace pseudocode_ide.interpreter.pseudocode
{
/// <summary>
/// Represents an autocomplete item for pseudocode with custom behavior for replacing text.
/// </summary>
public class PseudocodeAutocompleteItem : SnippetAutocompleteItem
{
/// <summary>
/// Initializes a new instance of the <see cref="PseudocodeAutocompleteItem"/> class with the specified snippet and optional menu text.
/// </summary>
/// <param name="snippet">The snippet to be used for autocomplete.</param>
/// <param name="menuText">The text to be displayed in the autocomplete menu. If null, the snippet is used.</param>
public PseudocodeAutocompleteItem(string snippet, string menuText = null) : base(snippet)
{
if (menuText != null)
{
MenuText = menuText;
}
}

/// <summary>
/// Gets the text to replace the current fragment with, adjusting for indentation based on the current line.
/// </summary>
/// <returns>The adjusted text for replacement.</returns>
public override string GetTextForReplace()
{
Scintilla scintilla = (Scintilla)Parent.TargetControlWrapper.TargetControl;

string line = scintilla.Lines[scintilla.LineFromPosition(Parent.Fragment.Start)].Text;
string indents = new string('\t', line.GetIndentationLevel());

return Text.Replace("\n", $"\n{indents}");
}
}
}
Loading

0 comments on commit 42cd6a8

Please sign in to comment.