diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs | 90 |
1 files changed, 84 insertions, 6 deletions
diff --git a/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs b/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs index 23933bf42..72696df10 100644 --- a/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs +++ b/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs @@ -1,4 +1,5 @@ using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; using System; @@ -22,6 +23,12 @@ namespace Tango.Scripting private OnExecuteParameters _onExecuteParameters; public List<Type> ReferencedAssemblies { get; private set; } + private class IncludesResult + { + public String File { get; set; } + public List<String> Lines { get; set; } + } + /// <summary> /// Initializes a new instance of the <see cref="ScriptEngine"/> class. /// </summary> @@ -41,7 +48,8 @@ namespace Tango.Scripting { try { - code = ReplaceIncludes(code, workingFolder); + List<IncludesResult> includeResults = new List<IncludesResult>(); + code = ReplaceIncludes(code, workingFolder, includeResults); } catch (Exception ex) { @@ -65,9 +73,11 @@ namespace Tango.Scripting { return Task.Factory.StartNew<List<CompilerError>>(() => { + List<IncludesResult> includeResults = new List<IncludesResult>(); + try { - code = ReplaceIncludes(code, workingFolder); + code = ReplaceIncludes(code, workingFolder, includeResults); } catch (Exception ex) { @@ -85,7 +95,41 @@ namespace Tango.Scripting var results = script.Compile(); - return results.Where(x => x.Severity == DiagnosticSeverity.Error).ToList().Select(x => new CompilerError() { Error = x.ToString() }).ToList(); + List<CompilerError> errors = new List<CompilerError>(); + + foreach (var result in results.Where(x => x.Severity == DiagnosticSeverity.Error)) + { + CompilerError error = new CompilerError(); + error.Error = result.GetMessage(); + error.Character = result.Location.GetMappedLineSpan().StartLinePosition.Character + 1; + + int lineIndex = 0; + IncludesResult include = null; + + + foreach (var inc in includeResults) + { + for (int i = 0; i < inc.Lines.Count; i++) + { + if (inc.Lines[i] == code.ToLines()[result.Location.GetMappedLineSpan().StartLinePosition.Line]) + { + include = inc; + lineIndex = i; + break; + } + } + } + + if (include != null) + { + error.File = Path.GetFileName(include.File); + error.Line = lineIndex + 1; + } + + errors.Add(error); + } + + return errors; }); } @@ -94,7 +138,7 @@ namespace Tango.Scripting return String.Join(", ", _onExecuteParameters.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance).Select(x => x.Name)); } - private String ReplaceIncludes(String code, String workingFolder) + private List<String> GetIncludes(String code, String workingFolder) { if (Directory.Exists(workingFolder)) { @@ -102,6 +146,36 @@ namespace Tango.Scripting } List<String> lines = code.ToLines(); + List<String> includeFiles = new List<string>(); + + for (int i = 0; i < lines.Count; i++) + { + var line = lines[i]; + + if (line.Trim().StartsWith("include")) + { + String path = line.Replace("include", "").Trim().Replace("\"", ""); + + if (!File.Exists(path)) + { + throw new FileNotFoundException("Could not locate include file '" + path + "'."); + } + + includeFiles.Add(path); + } + } + + return includeFiles; + } + + private String ReplaceIncludes(String code, String workingFolder, List<IncludesResult> includeResults) + { + if (Directory.Exists(workingFolder)) + { + Environment.CurrentDirectory = workingFolder; + } + + List<String> lines = code.ToLines().ToList(); for (int i = 0; i < lines.Count; i++) { @@ -116,7 +190,11 @@ namespace Tango.Scripting throw new FileNotFoundException("Could not locate include file '" + path + "'."); } - String content = ReplaceIncludes(File.ReadAllText(path), Path.GetDirectoryName(path)); + String includeContent = File.ReadAllText(path); + + includeResults.Add(new IncludesResult() { File = path, Lines = includeContent.ToLines() }); + + String content = ReplaceIncludes(includeContent, Path.GetDirectoryName(path), includeResults); if (content.Contains("OnExecute(")) { @@ -148,7 +226,7 @@ namespace Tango.Scripting lines.RemoveAll(x => x.Trim().StartsWith("using")); - return String.Join(Environment.NewLine, usings.Distinct()) + String.Join(Environment.NewLine, lines); + return String.Join(Environment.NewLine, usings.Distinct()) + Environment.NewLine + String.Join(Environment.NewLine, lines); } private ScriptOptions CreateOptions() |
