Skip to content

Commit

Permalink
Add button to copy c# code to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
PocketMiner82 committed Feb 12, 2024
1 parent a759e7c commit 5ba5504
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 24 deletions.
2 changes: 1 addition & 1 deletion AutoUpdater.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.2.1.0</version>
<version>1.2.1.1</version>
<url>https://github.com/PocketMiner82/pseudocode-ide/releases/latest/download/pseudocode-ide.zip</url>
<changelog>https://github.com/PocketMiner82/pseudocode-ide/releases</changelog>
<mandatory>false</mandatory>
Expand Down
4 changes: 2 additions & 2 deletions SolutionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;

[assembly: AssemblyVersion("1.2.1.0")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyVersion("1.2.1.1")]
[assembly: AssemblyFileVersion("1.2.1.1")]
87 changes: 87 additions & 0 deletions pseudocodeIde/SetClipboardHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pseudocode_ide
{
// from https://stackoverflow.com/questions/899350/how-do-i-copy-the-contents-of-a-string-to-the-clipboard-in-c

public class SetClipboardHelper : StaHelper
{
readonly string _format;
readonly object _data;

public SetClipboardHelper(string format, object data)
{
_format = format;
_data = data;
}

protected override void Work()
{
var obj = new System.Windows.Forms.DataObject(
_format,
_data
);

Clipboard.SetDataObject(obj, true);
}
}

public abstract class StaHelper
{
readonly ManualResetEvent _complete = new ManualResetEvent(false);

public void Go()
{
var thread = new Thread(new ThreadStart(DoWork))
{
IsBackground = true,
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

// Thread entry method
private void DoWork()
{
try
{
_complete.Reset();
Work();
}
catch (Exception ex)
{
if (DontRetryWorkOnFailed)
throw;
else
{
try
{
Thread.Sleep(1000);
Work();
}
catch
{
// ex from first exception
Debug.WriteLine(ex);
}
}
}
finally
{
_complete.Set();
}
}

public bool DontRetryWorkOnFailed { get; set; }

// Implemented in base class to do actual work.
protected abstract void Work();
}
}
9 changes: 6 additions & 3 deletions pseudocodeIde/interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class Interpreter

private Thread programThread = null;

public CSharpCode cSharpCode { get; private set; } = null;

private string code
{
get
Expand Down Expand Up @@ -67,14 +69,15 @@ private bool tryRun()
Logger.info($"Generierte Tokens:\n{tokensString}\n\n");

Parser parser = new Parser(tokens);
CSharpCode cSharpCode = parser.parseTokens();
this.cSharpCode = parser.parseTokens();
outputForm.showCopyButton();

if (this.cancelRequestedOrError())
{
return false;
}

cSharpCode.compile();
this.cSharpCode.compile();

if (this.cancelRequestedOrError())
{
Expand All @@ -84,7 +87,7 @@ private bool tryRun()
Logger.info(LogMessage.RUNNING_PROGRAM);
Logger.print("");

this.programThread = new Thread(cSharpCode.execute);
this.programThread = new Thread(this.cSharpCode.execute);
this.programThread.Start();
return true;
}
Expand Down
33 changes: 21 additions & 12 deletions pseudocodeIde/interpreter/OutputForm.Designer.cs

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

18 changes: 17 additions & 1 deletion pseudocodeIde/interpreter/OutputForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using pseudocodeIde.interpreter;
using pseudocode_ide;
using pseudocodeIde.interpreter;
using pseudocodeIde.interpreter.logging;
using System;
using System.Threading;
Expand Down Expand Up @@ -93,6 +94,7 @@ public void ShowAndRun()

private void startMenuItem_Click(object sender, EventArgs e)
{
copyCSharpCodeMenuItem.Visible = false;
this.stopMenuItem_Click(null, null);

startMenuItem.Enabled = false;
Expand Down Expand Up @@ -123,5 +125,19 @@ public void stopMenuItem_Click(object sender, EventArgs e)
Logger.print("");
Logger.info(LogMessage.STOPPED_PROGRAM);
}

private void copyCSharpCodeMenuItem_Click(object sender, EventArgs e)
{
new SetClipboardHelper(DataFormats.UnicodeText, this.interpreter.cSharpCode.codeText).Go();
MessageBox.Show("C# Code in Zwischenablage gespeichert.", "Zwischenablage");
}

public void showCopyButton()
{
Invoke(new Action(() =>
{
copyCSharpCodeMenuItem.Visible = true;
}));
}
}
}
12 changes: 7 additions & 5 deletions pseudocodeIde/interpreter/parser/CSharpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public CodeOutput(Action<string, bool> printMethod) : base(printMethod) {

public string methods { get; set; } = "";

public string codeText { get; private set; } = "";

private Assembly compiledAssembly;


Expand All @@ -122,20 +124,20 @@ public void compile()
parameters.CompilerOptions += "/unsafe /optimize /langversion:9.0";


string code = TEMPLATE_CLASS
this.codeText = TEMPLATE_CLASS
.Replace("%FIELDS%", this.fields)
.Replace("%CONSTRUCTOR%", this.constructor)
.Replace("%METHODS%", this.methods);

// pretty print code
SyntaxNode node = CSharpSyntaxTree.ParseText(code).GetRoot();
code = node.NormalizeWhitespace().ToFullString();
SyntaxNode node = CSharpSyntaxTree.ParseText(this.codeText).GetRoot();
this.codeText = node.NormalizeWhitespace().ToFullString();

Logger.info(LogMessage.GENERATED_C_SHARP_CODE);

string printCode = "1\t";
int line = 1;
foreach(char c in code)
foreach(char c in this.codeText)
{
if (c == '\n')
{
Expand All @@ -150,7 +152,7 @@ public void compile()

Logger.info(LogMessage.COMPILING_C_SHARP_CODE);

CompilerResults result = provider.CompileAssemblyFromSource(parameters, code);
CompilerResults result = provider.CompileAssemblyFromSource(parameters, this.codeText);

if (result.Errors.Count > 0)
{
Expand Down
1 change: 1 addition & 0 deletions pseudocodeIde/pseudocodeIde.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<Compile Include="interpreter\OutputForm.Designer.cs">
<DependentUpon>OutputForm.cs</DependentUpon>
</Compile>
<Compile Include="SetClipboardHelper.cs" />
<EmbeddedResource Include="findReplace\FindReplaceForm.resx">
<DependentUpon>FindReplaceForm.cs</DependentUpon>
</EmbeddedResource>
Expand Down

0 comments on commit 5ba5504

Please sign in to comment.