Skip to content

Commit

Permalink
Fix /compile having wrong error line number when source code hs 'refe…
Browse files Browse the repository at this point in the history
…rence' statements at start of file
  • Loading branch information
UnknownShadow200 committed Mar 28, 2020
1 parent 985294f commit 89221b1
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions MCGalaxy/Scripting/Scripting.cs
Expand Up @@ -83,7 +83,8 @@ public abstract class IScripting {
args.GenerateExecutable = false;
args.OutputAssembly = dstPath;

List<string> source = ReadSource(srcPath, args);
int offset = 0;
List<string> source = ReadSource(srcPath, args, ref offset);
CompilerResults results = CompileSource(source.Join(Environment.NewLine), args);
if (!results.Errors.HasErrors) return true;

Expand All @@ -95,7 +96,7 @@ public abstract class IScripting {

foreach (CompilerError err in results.Errors) {
string type = err.IsWarning ? "Warning" : "Error";
sb.AppendLine(type + " on line " + err.Line + ":");
sb.AppendLine(type + " on line " + (err.Line + offset) + ":");

if (err.Line > 0) sb.AppendLine(source[err.Line - 1]);
if (err.Column > 0) sb.Append(' ', err.Column - 1);
Expand All @@ -109,7 +110,7 @@ public abstract class IScripting {
int logged = 0;
foreach (CompilerError err in results.Errors) {
string type = err.IsWarning ? "Warning" : "Error";
p.Message("%W{0} #{1} on line {2} - {3}", type, err.ErrorNumber, err.Line, err.ErrorText);
p.Message("%W{0} #{1} on line {2} - {3}", type, err.ErrorNumber, err.Line + offset, err.ErrorText);

logged++;
if (logged >= maxLog) break;
Expand All @@ -122,7 +123,7 @@ public abstract class IScripting {
return false;
}

static List<string> ReadSource(string path, CompilerParameters args) {
static List<string> ReadSource(string path, CompilerParameters args, ref int offset) {
List<string> lines = Utils.ReadAllLinesList(path);
// Allow referencing other assemblies using 'Reference [assembly name]' at top of the file
for (int i = 0; i < lines.Count; i++) {
Expand All @@ -131,7 +132,8 @@ public abstract class IScripting {
int index = lines[i].IndexOf(' ') + 1;
string assem = lines[i].Substring(index);
args.ReferencedAssemblies.Add(assem);
lines.RemoveAt(i); i--;
lines.RemoveAt(i);
offset++; i--;
}
return lines;
}
Expand Down

0 comments on commit 89221b1

Please sign in to comment.