static void PreprocessSnippet(ref SnippetInfo info, String snippetText) { string[] lines = snippetText.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int snippetStart = 0; for (int i = 0; i < lines.Length; i++) { string l = lines[i].Trim(); if (!String.IsNullOrEmpty(l)) { if (l.StartsWith(">>")) { l = l.Substring(2); try { parseOption(ref info, l); } catch (Exception ex) { throw new Exception("Exception parsing option >>" + l + " : " + ex.Message, ex); } } else { snippetStart = i; break; } } } int snippetEnd = lines.Length; for (int i = snippetStart; i < lines.Length; i++) { if (lines[i].StartsWith("--- SnippetExecutor")) { snippetEnd = i; break; } } StringBuilder sb = new StringBuilder(); for (int i = snippetStart; i < snippetEnd; i++) { sb.AppendLine(lines[i]); } info.postprocessed = sb.ToString(); return; }
private static IO ioForOption(String option, SnippetInfo info) { option = option.TrimStart(); if (String.IsNullOrEmpty(option)) { throw new Exception("No IO destination specified"); } if (option.StartsWith("console", StringComparison.OrdinalIgnoreCase)) { return(frmMyDlg.getIOToConsole()); } else if (option.StartsWith("append", StringComparison.OrdinalIgnoreCase)) { return(new IOAppendCurrentDoc()); } else if (option.StartsWith("insert", StringComparison.OrdinalIgnoreCase)) { return(new IOInsertAtPosition()); } else if (option.StartsWith("new", StringComparison.OrdinalIgnoreCase)) { return(IONewDoc.NewDocFactory()); } else if (option.StartsWith("file", StringComparison.OrdinalIgnoreCase)) { string filename = option.Substring(4, option.Length - 4); if (!Path.IsPathRooted(filename)) { filename = Path.Combine(info.workingDirectory, filename); } //create an IO to that document return(IOFileDoc.FileDocFactory(filename)); } else { //TODO: new IOWriteDoc(); throw new NotImplementedException("silent file"); } }
static void PreprocessSnippet(ref SnippetInfo info, String snippetText) { string[] lines = snippetText.Split(new String[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries); int snippetStart = 0; for (int i = 0; i < lines.Length; i++) { string l = lines[i].Trim(); if (!String.IsNullOrEmpty(l)) { if (l.StartsWith(">>")) { l = l.Substring(2); try { parseOption(ref info, l); } catch (Exception ex) { throw new Exception("Exception parsing option >>" + l + " : " + ex.Message, ex); } } else { snippetStart = i; break; } } } int snippetEnd = lines.Length; for (int i = snippetStart; i < lines.Length; i++) { if (lines[i].StartsWith("--- SnippetExecutor")) { snippetEnd = i; break; } } StringBuilder sb = new StringBuilder(); for (int i = snippetStart; i < snippetEnd; i++) { sb.AppendLine(lines[i]); } info.postprocessed = sb.ToString(); return; }
static void parseOption(ref SnippetInfo info, String option) { option.Trim(); string[] cmdOps = option.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (cmdOps.Length == 0) return; String cmd = cmdOps[0].ToLower(); switch (cmd) { case "lang": if (cmdOps.Length < 2) throw new Exception("no language specified"); string lang = "L_" + cmdOps[1].Trim().ToUpper(); bool success = Enum.TryParse<LangType>(lang, out info.language); break; case "out": if (cmdOps.Length < 2) throw new Exception("no output specified"); info.stdIO = ioForOption(option.Substring(cmdOps[0].Length, option.Length - cmdOps[0].Length), info); break; case "console": if (cmdOps.Length < 2) throw new Exception("no output specified"); info.console = ioForOption(option.Substring(cmdOps[0].Length, option.Length - cmdOps[0].Length), info); break; case "working": if (cmdOps.Length < 2) throw new Exception("no directory specified"); string dir = cmd.Substring(cmd.Length).Trim(); if(!Directory.Exists(dir)) throw new Exception("directory " + Path.GetFullPath(dir) + " doesn't exist"); else info.workingDirectory = dir; break; default: //shove it in the hashtable string s = (string)info.options[cmd]; if (! String.IsNullOrEmpty(s)){ if (option.Length > cmd.Length) { //multiple lines with the same option: append them to one line info.options[cmd] = String.Concat(s, " ", option.Substring(cmd.Length).Trim()); } } else { if (option.Length > cmd.Length) info.options[cmd] = option.Substring(cmd.Length).Trim(); else info.options[cmd] = String.Empty; } break; } }
private static IO ioForOption(String option, SnippetInfo info) { option = option.TrimStart(); if (String.IsNullOrEmpty(option)) { throw new Exception("No IO destination specified"); } if (option.StartsWith("console", StringComparison.OrdinalIgnoreCase)) { return frmMyDlg.getIOToConsole(); } else if (option.StartsWith("append", StringComparison.OrdinalIgnoreCase)) { return new IOAppendCurrentDoc(); } else if (option.StartsWith("insert", StringComparison.OrdinalIgnoreCase)) { return new IOInsertAtPosition(); } else if (option.StartsWith("new", StringComparison.OrdinalIgnoreCase)) { return IONewDoc.NewDocFactory(); } else if (option.StartsWith("file", StringComparison.OrdinalIgnoreCase)) { string filename = option.Substring(4, option.Length - 4); if (!Path.IsPathRooted(filename)) { filename = Path.Combine(info.workingDirectory, filename); } //create an IO to that document return IOFileDoc.FileDocFactory(filename); } else { //TODO: new IOWriteDoc(); throw new NotImplementedException("silent file"); } }
internal static void CompileSnippet() { Logger log = null; try { log = Logger.CreateLogger(); try { IntPtr currScint = PluginBase.GetCurrentScintilla(); myDockableDialog(); IO console = frmMyDlg.getIOToConsole(); log.Log("compile snippet"); int len = (int) Win32.SendMessage(currScint, SciMsg.SCI_GETSELTEXT, 0, 0); StringBuilder text; if (len > 1) { //a selection exists text = new StringBuilder(len); Win32.SendMessage(currScint, SciMsg.SCI_GETSELTEXT, 0, text); } else { //no selection, parse whole file len = (int) Win32.SendMessage(currScint, SciMsg.SCI_GETTEXT, 0, 0); text = new StringBuilder(len); Win32.SendMessage(currScint, SciMsg.SCI_GETTEXT, len, text); } if (text.Length == 0) { console.writeLine("No Text"); return; } //create defaults SnippetInfo info = new SnippetInfo(); info.language = LangType.L_TEXT; int langtype = (int) LangType.L_TEXT; Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTLANGTYPE, 0, out langtype); info.language = (LangType) langtype; info.stdIO = console; info.console = console; info.preprocessed = text.ToString(); info.runCmdLine = String.Empty; info.compilerCmdLine = String.Empty; info.options = new Hashtable(); StringBuilder sb = new StringBuilder(Win32.MAX_PATH); Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTDIRECTORY, Win32.MAX_PATH, sb); info.workingDirectory = sb.ToString(); //process overrides try { PreprocessSnippet(ref info, text.ToString()); } catch (Exception ex) { console.writeLine("\r\n\r\n--- SnippetExecutor " + DateTime.Now.ToShortTimeString() + " ---"); console.writeLine("Exception processing snippet"); Main.HandleException(console, ex); return; } console = info.console; console.writeLine("\r\n\r\n--- SnippetExecutor " + DateTime.Now.ToShortTimeString() + " ---"); foreach (DictionaryEntry pair in info.options) { console.writeLine(pair.Key.ToString() + ":" + pair.Value.ToString()); } //get correct compiler for language info.compiler = getCompilerForLanguage(info.language); info.compiler.console = info.console; info.compiler.stdIO = info.stdIO; foreach (DictionaryEntry e in info.options) { info.compiler.options.Add(e.Key, e.Value); } Thread th = new Thread( delegate() { Logger logInner = Logger.CreateLogger(); try { console.writeLine("-- Generating source for snippet..."); info.postprepared = info.compiler.PrepareSnippet(info.postprocessed); if (info.options.ContainsKey("source")) { IO writer = console; writer.writeLine(); if (!String.IsNullOrEmpty((string) info.options["source"])) { string opt = (info.options["source"] as string); if (!String.IsNullOrEmpty(opt)) { try { writer = ioForOption(opt, info); } catch (Exception ex) { console.writeLine("Cannot write to " + opt); console.writeLine(ex.Message); return; } } } writer.write(info.postprepared); } if (String.IsNullOrEmpty(info.postprepared)) return; info.compilerCmdLine = (string) info.options["compile"]; console.writeLine("\r\n-- compiling source with options " + info.compilerCmdLine); info.executable = info.compiler.Compile(info.postprepared, info.compilerCmdLine); if (info.executable == null) return; EventHandler cancelDelegate = delegate(object sender, EventArgs e) { info.compiler.Cancel(); console.write("-- Cancelling --"); }; frmMyDlg.CancelRunButtonClicked += cancelDelegate; info.compiler.workingDirectory = info.workingDirectory; info.runCmdLine = (string) info.options["run"]; console.writeLine("-- running with options " + info.runCmdLine + " --"); info.compiler.execute(info.executable, info.runCmdLine); console.writeLine("\r\n-- finished run --"); frmMyDlg.CancelRunButtonClicked -= cancelDelegate; } catch (Exception ex) { Main.HandleException(console, ex); return; } finally { if (info.executable != null) { info.compiler.cleanup(info); } console.Dispose(); info.stdIO.Dispose(); logInner.Dispose(); } } ); th.Start(); } catch (Exception ex) { if (Main.debug != null) { Main.HandleException(Main.debug, ex); } else { MessageBox.Show(ex.ToString()); } return; } } catch(Exception ex) { MessageBox.Show("Error creating logger: " + ex.ToString()); } finally { if (log != null) log.Dispose(); } }
public abstract bool cleanup(SnippetInfo info);
static void parseOption(ref SnippetInfo info, String option) { option.Trim(); string[] cmdOps = option.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (cmdOps.Length == 0) { return; } String cmd = cmdOps[0].ToLower(); switch (cmd) { case "lang": if (cmdOps.Length < 2) { throw new Exception("no language specified"); } string lang = "L_" + cmdOps[1].Trim().ToUpper(); bool success = Enum.TryParse <LangType>(lang, out info.language); break; case "out": if (cmdOps.Length < 2) { throw new Exception("no output specified"); } info.stdIO = ioForOption(option.Substring(cmdOps[0].Length, option.Length - cmdOps[0].Length), info); break; case "console": if (cmdOps.Length < 2) { throw new Exception("no output specified"); } info.console = ioForOption(option.Substring(cmdOps[0].Length, option.Length - cmdOps[0].Length), info); break; case "working": if (cmdOps.Length < 2) { throw new Exception("no directory specified"); } string dir = cmd.Substring(cmd.Length).Trim(); if (!Directory.Exists(dir)) { throw new Exception("directory " + Path.GetFullPath(dir) + " doesn't exist"); } else { info.workingDirectory = dir; } break; default: //shove it in the hashtable string s = (string)info.options[cmd]; if (!String.IsNullOrEmpty(s)) { if (option.Length > cmd.Length) { //multiple lines with the same option: append them to one line info.options[cmd] = String.Concat(s, " ", option.Substring(cmd.Length).Trim()); } } else { if (option.Length > cmd.Length) { info.options[cmd] = option.Substring(cmd.Length).Trim(); } else { info.options[cmd] = String.Empty; } } break; } }
internal static void CompileSnippet() { Logger log = null; try { log = Logger.CreateLogger(); try { IntPtr currScint = PluginBase.GetCurrentScintilla(); myDockableDialog(); IO console = frmMyDlg.getIOToConsole(); log.Log("compile snippet"); int len = (int)Win32.SendMessage(currScint, SciMsg.SCI_GETSELTEXT, 0, 0); StringBuilder text; if (len > 1) { //a selection exists text = new StringBuilder(len); Win32.SendMessage(currScint, SciMsg.SCI_GETSELTEXT, 0, text); } else { //no selection, parse whole file len = (int)Win32.SendMessage(currScint, SciMsg.SCI_GETTEXT, 0, 0); text = new StringBuilder(len); Win32.SendMessage(currScint, SciMsg.SCI_GETTEXT, len, text); } if (text.Length == 0) { console.writeLine("No Text"); return; } //create defaults SnippetInfo info = new SnippetInfo(); info.language = LangType.L_TEXT; int langtype = (int)LangType.L_TEXT; Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTLANGTYPE, 0, out langtype); info.language = (LangType)langtype; info.stdIO = console; info.console = console; info.preprocessed = text.ToString(); info.runCmdLine = String.Empty; info.compilerCmdLine = String.Empty; info.options = new Hashtable(); StringBuilder sb = new StringBuilder(Win32.MAX_PATH); Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTDIRECTORY, Win32.MAX_PATH, sb); info.workingDirectory = sb.ToString(); //process overrides try { PreprocessSnippet(ref info, text.ToString()); } catch (Exception ex) { console.writeLine("\r\n\r\n--- SnippetExecutor " + DateTime.Now.ToShortTimeString() + " ---"); console.writeLine("Exception processing snippet"); Main.HandleException(console, ex); return; } console = info.console; console.writeLine("\r\n\r\n--- SnippetExecutor " + DateTime.Now.ToShortTimeString() + " ---"); foreach (DictionaryEntry pair in info.options) { console.writeLine(pair.Key.ToString() + ":" + pair.Value.ToString()); } //get correct compiler for language info.compiler = getCompilerForLanguage(info.language); info.compiler.console = info.console; info.compiler.stdIO = info.stdIO; foreach (DictionaryEntry e in info.options) { info.compiler.options.Add(e.Key, e.Value); } Thread th = new Thread( delegate() { Logger logInner = Logger.CreateLogger(); try { console.writeLine("-- Generating source for snippet..."); info.postprepared = info.compiler.PrepareSnippet(info.postprocessed); if (info.options.ContainsKey("source")) { IO writer = console; writer.writeLine(); if (!String.IsNullOrEmpty((string)info.options["source"])) { string opt = (info.options["source"] as string); if (!String.IsNullOrEmpty(opt)) { try { writer = ioForOption(opt, info); } catch (Exception ex) { console.writeLine("Cannot write to " + opt); console.writeLine(ex.Message); return; } } } writer.write(info.postprepared); } if (String.IsNullOrEmpty(info.postprepared)) { return; } info.compilerCmdLine = (string)info.options["compile"]; console.writeLine("\r\n-- compiling source with options " + info.compilerCmdLine); info.executable = info.compiler.Compile(info.postprepared, info.compilerCmdLine); if (info.executable == null) { return; } EventHandler cancelDelegate = delegate(object sender, EventArgs e) { info.compiler.Cancel(); console.write("-- Cancelling --"); }; frmMyDlg.CancelRunButtonClicked += cancelDelegate; info.compiler.workingDirectory = info.workingDirectory; info.runCmdLine = (string)info.options["run"]; console.writeLine("-- running with options " + info.runCmdLine + " --"); info.compiler.execute(info.executable, info.runCmdLine); console.writeLine("\r\n-- finished run --"); frmMyDlg.CancelRunButtonClicked -= cancelDelegate; } catch (Exception ex) { Main.HandleException(console, ex); return; } finally { if (info.executable != null) { info.compiler.cleanup(info); } console.Dispose(); info.stdIO.Dispose(); logInner.Dispose(); } } ); th.Start(); } catch (Exception ex) { if (Main.debug != null) { Main.HandleException(Main.debug, ex); } else { MessageBox.Show(ex.ToString()); } return; } } catch (Exception ex) { MessageBox.Show("Error creating logger: " + ex.ToString()); } finally { if (log != null) { log.Dispose(); } } }
public abstract bool cleanup(SnippetInfo info);