Permalink
Browse files

Set up basic error highlighting

  • Loading branch information...
1 parent 47d0b2a commit f83af6844958e0b886810f7d849417dcc401bed6 @jrusbatch committed Apr 19, 2012
@@ -1,17 +1,17 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
+using Roslyn.Compilers.Common;
namespace Compilify.Services
{
public class CSharpCompiler
{
- public IEnumerable<string> GetCompilationErrors(string command, string classes)
+ public IEnumerable<IDiagnostic> GetCompilationErrors(string command, string classes)
{
- var errors = new List<string>();
-
var script = "public static object Eval() {" + command + "}";
var mscorlib = Assembly.Load("mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");
@@ -34,8 +34,8 @@ public IEnumerable<string> GetCompilationErrors(string command, string classes)
new[]
{
SyntaxTree.ParseCompilationUnit(CodeExecuter.EntryPoint),
- SyntaxTree.ParseCompilationUnit(script, options: new ParseOptions(kind: SourceCodeKind.Interactive)),
- SyntaxTree.ParseCompilationUnit(classes ?? string.Empty, options: new ParseOptions(kind: SourceCodeKind.Script))
+ SyntaxTree.ParseCompilationUnit(script, fileName: "Prompt", options: new ParseOptions(kind: SourceCodeKind.Interactive)),
+ SyntaxTree.ParseCompilationUnit(classes ?? string.Empty, fileName: "Editor", options: new ParseOptions(kind: SourceCodeKind.Script))
},
new MetadataReference[]
{
@@ -44,9 +44,7 @@ public IEnumerable<string> GetCompilationErrors(string command, string classes)
new AssemblyFileReference(system.Location)
});
- errors.AddRange(compilation.GetDiagnostics().Select(x => x.Info.GetMessage().Replace("Eval()", "<Factory>()")));
-
- return errors;
+ return compilation.GetDiagnostics();
}
}
}
@@ -20,6 +20,10 @@
font-style: normal;
}
+html {
+ min-width: 1006px;
+}
+
body {
font-size: 14px;
}
@@ -247,4 +251,8 @@ h1 {
font-size: 200px;
margin: 20px 0;
line-height: 200px;
+}
+
+.editor .compilation-error {
+ background-color: #f00;
}
View
@@ -87,6 +87,8 @@ if (typeof String.prototype.trim !== 'function') {
return false;
}
+ var markedErrors = [];
+
function validate(command, classes) {
/// <summary>
/// Sends code to the server for validation and displays the resulting
@@ -101,6 +103,12 @@ if (typeof String.prototype.trim !== 'function') {
data: JSON.stringify({ 'Command': command, 'Classes': classes }),
success: function(msg) {
var data = msg.data;
+
+ for (var i = markedErrors.length - 1; i >= 0; i--) {
+ markedErrors[i].clear();
+ }
+
+ markedErrors.length = 0;
if (_.isArray(data)) {
var $list = $('.messages ul').detach().empty();
@@ -111,8 +119,19 @@ if (typeof String.prototype.trim !== 'function') {
} else {
for (var i in msg.data) {
var error = msg.data[i];
+
+ var start = error.Location.StartLinePosition;
+ var end = error.Location.EndLinePosition;
+
+ var markStart = { line: start.Line, ch: start.Character };
+ var markEnd = { line: end.Line, ch: end.Character };
+
+ var mark = Compilify[error.Location.FileName].markText(markStart, markEnd, 'compilation-error');
+
+ markedErrors.push(mark);
+
$list.append('<li class="message error">' +
- htmlEscape(error) + '</li>');
+ htmlEscape(error.Message) + '</li>');
}
}
@@ -1,14 +1,11 @@
-using System;
-using System.Linq;
+using System.Linq;
using System.Text;
-using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Compilify.Models;
using Compilify.Services;
using Compilify.Web.Models;
using Compilify.Web.Services;
-using Roslyn.Compilers.CSharp;
namespace Compilify.Web.Controllers
{
@@ -59,11 +56,18 @@ public ActionResult Index()
commandBuilder.AppendLine("return person.Greet();");
var post = new Post { Content = commandBuilder.ToString(), Classes = classesBuilder.ToString() };
+
+ var errors = compiler.GetCompilationErrors(post.Content, post.Classes)
+ .Select(x => new EditorError
+ {
+ Location = x.Location.GetLineSpan(true),
+ Message = x.Info.GetMessage()
+ });
var viewModel = new PostViewModel
{
Post = post,
- Errors = compiler.GetCompilationErrors(post.Content, post.Classes)
+ Errors = errors
};
return View("Show", viewModel);
@@ -99,10 +103,17 @@ public ActionResult Show(string slug, int? version)
return View("Error");
}
+ var errors = compiler.GetCompilationErrors(post.Content, post.Classes)
+ .Select(x => new EditorError
+ {
+ Location = x.Location.GetLineSpan(true),
+ Message = x.Info.GetMessage()
+ });
+
var viewModel = new PostViewModel
{
Post = post,
- Errors = compiler.GetCompilationErrors(post.Content, post.Classes)
+ Errors = errors
};
if (Request.IsAjaxRequest())
@@ -145,7 +156,11 @@ public ActionResult Save(string slug, Post post)
public ActionResult Validate(ValidateViewModel viewModel)
{
var errors = compiler.GetCompilationErrors(viewModel.Command, viewModel.Classes)
- .ToArray();
+ .Select(x => new EditorError
+ {
+ Location = x.Location.GetLineSpan(true),
+ Message = x.Info.GetMessage()
+ });
return Json(new { status = "ok", data = errors });
}
@@ -8,11 +8,11 @@ public class PostViewModel
public PostViewModel()
{
Post = new Post();
- Errors = new List<string>();
+ Errors = new List<EditorError>();
}
public Post Post { get; set; }
- public IEnumerable<string> Errors { get; set; }
+ public IEnumerable<EditorError> Errors { get; set; }
}
}
@@ -1,8 +1,28 @@
-namespace Compilify.Web.Models
+using Newtonsoft.Json;
+using Roslyn.Compilers;
+
+namespace Compilify.Web.Models
{
public class ValidateViewModel
{
public string Command { get; set; }
public string Classes { get; set; }
}
+
+ [JsonObject]
+ public class EditorError
+ {
+ public FileLinePositionSpan Location { get; set; }
+ public string Message { get; set; }
+ }
+
+ [JsonObject]
+ public class EditorLocation
+ {
+ [JsonProperty("line")]
+ public int Line { get; set; }
+
+ [JsonProperty("ch")]
+ public int Character { get; set; }
+ }
}

0 comments on commit f83af68

Please sign in to comment.