Manually add errors to a resource [message #1406587] |
Sun, 10 August 2014 14:16 |
tly . Messages: 8 Registered: August 2014 |
Junior Member |
|
|
In my attempt to create an editor for the GLSL shading language im facing different problems. I would like to show errors reported by the native compiler in the editor.
I managed to create an option to compile the shader and show the error log in a console. Then i added these errors to the resource to highlight the specific line in the editor. Unfortunately the errors dont show up.
This is my attempt: I made a LinedDiagnostic class to implement the Diagnostic interface needed for the resource.
public static class LinedDiagnostic implements Resource.Diagnostic{
final String message;
final int line;
final String resourceURI;
public LinedDiagnostic(String message, int line,String resourceURI) {
this.message = message;
this.line = line;
this.resourceURI=resourceURI;
}
@Override
public String getMessage() {
return message;
}
@Override
public String getLocation() {
return resourceURI;
}
@Override
public int getLine() {
return line;
}
@Override
public int getColumn() {
return 1;
}
}
and then added the Errors to the resource:
public static void addErrorsToResource(String errors,Resource r){
Stream.of(errors.split("\n"))
.skip(1)
.map(line->CompilerError.parseError(line))
.filter(e->e!=null)
.forEach(
e->{
System.out.println("Adding error: "+e);
r.getErrors().add(new Internals.LinedDiagnostic(e.text, e.line, r.getURI().toString()));
}
);
}
this is called by a popupAction:
MessageConsole compilerConsole=null;
public void run(IAction action) {
IFile shaderFile=(IFile) ((TreeSelection) selection).getFirstElement();
if(compilerConsole==null){
compilerConsole=createConsole("GLSL Compiler");
}
MessageConsoleStream out=compilerConsole.newMessageStream();
try {
int shaderType=ShaderDialog.getShaderType(shell, shaderFile);
if(shaderType==-1) return;
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();// obtain the active page
String id = IConsoleConstants.ID_CONSOLE_VIEW;
IConsoleView view;
view = (IConsoleView) page.showView(id);
view.display(compilerConsole);
out.println("Compiling shader...");
String errors=CompilerService.checkForErrors(shaderFile.getLocation().toString(),shaderType);
if(errors.isEmpty()){
out.println("No Errors occured.");
}else{
out.println(errors);
Resource r=resourceSetProvider.get(shaderFile.getProject())
.getResource(URI.createURI(shaderFile.getFullPath().toString()), true);
CompilerService.addErrorsToResource(errors, r);
}
} catch (CompilerException | IOException | PartInitException e) {
out.println("[Internal Error] "+e.getMessage());
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
|
|
|
Re: Manually add errors to a resource [message #1406616 is a reply to message #1406587] |
Sun, 10 August 2014 16:16 |
Ed Willink Messages: 7655 Registered: July 2009 |
Senior Member |
|
|
Hi
Resource.errors is a good place to put your model errors, but the
Eclipse UI is to use org.eclipse.core.resources.IMarker so you need to
maintain the markers or reuse some functionality that does so for you.
e.g org.eclipse.emf.validation.marker.MarkerUtil,
org.eclipse.emf.edit.ui.util.EditUIMarkerHelper
Regards
Ed Willink
On 10/08/2014 15:44, tly . wrote:
> In my attempt to create an editor for the GLSL shading language im
> facing different problems. I would like to show errors reported by the
> native compiler in the editor.
>
> I managed to create an option to compile the shader and show the error
> log in a console. Then i added these errors to the resource to
> highlight the specific line in the editor. Unfortunately the errors
> dont show up.
>
> This is my attempt: I made a LinedDiagnostic class to implement the
> Diagnostic interface needed for the resource.
>
>
> public static class LinedDiagnostic implements Resource.Diagnostic{
> final String message;
> final int line;
> final String resourceURI;
>
> public LinedDiagnostic(String message, int line,String resourceURI) {
> this.message = message;
> this.line = line;
> this.resourceURI=resourceURI;
> }
>
> @Override
> public String getMessage() {
> return message;
> }
>
> @Override
> public String getLocation() {
> return resourceURI;
> }
>
> @Override
> public int getLine() {
> return line;
> }
>
> @Override
> public int getColumn() {
> return 1;
> }
>
> }
>
>
> and then added the Errors to the resource:
>
> public static void addErrorsToResource(String errors,Resource r){
> Stream.of(errors.split("\n"))
> .skip(1)
> .map(line->CompilerError.parseError(line))
> .filter(e->e!=null)
> .forEach(
> e->{
> System.out.println("Adding error: "+e);
> r.getErrors().add(new Internals.LinedDiagnostic(e.text,
> e.line, r.getURI().toString()));
> }
> );
> }
>
> this is called by a popupAction:
>
> MessageConsole compilerConsole=null;
>
> public void run(IAction action) {
> IFile shaderFile=(IFile) ((TreeSelection)
> selection).getFirstElement();
> if(compilerConsole==null){
> compilerConsole=createConsole("GLSL Compiler");
> }
> MessageConsoleStream out=compilerConsole.newMessageStream();
> try {
> int shaderType=ShaderDialog.getShaderType(shell, shaderFile);
> if(shaderType==-1) return;
> IWorkbenchPage page =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();// obtain
> the active page
> String id = IConsoleConstants.ID_CONSOLE_VIEW;
> IConsoleView view;
> view = (IConsoleView) page.showView(id);
> view.display(compilerConsole);
> out.println("Compiling shader...");
> String
> errors=CompilerService.checkForErrors(shaderFile.getLocation().toString(),shaderType);
> if(errors.isEmpty()){
> out.println("No Errors occured.");
> }else{
> out.println(errors);
> Resource r=resourceSetProvider.get(shaderFile.getProject())
> .getResource(URI.createURI(shaderFile.getFullPath().toString()), true);
> CompilerService.addErrorsToResource(errors, r); }
> } catch (CompilerException | IOException | PartInitException e) {
> out.println("[Internal Error] "+e.getMessage());
> }
> try {
> out.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
|
|
|
Re: Manually add errors to a resource [message #1407106 is a reply to message #1406616] |
Mon, 11 August 2014 22:22 |
tly . Messages: 8 Registered: August 2014 |
Junior Member |
|
|
thank you for your help! Of course a nice feature like Resource.recreateMarkersFromErrors() would have been nicer
this is my approach:
if(errors.isEmpty()){
out.println("No Errors occured.");
}else{
out.println(errors);
Resource r=resourceSetProvider.get(shaderFile.getProject())
.getResource(URI.createURI(shaderFile.getFullPath().toString()), true);
try {
shaderFile.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
shaderFile.deleteMarkers("org.glsl.ui.glsl.check.fast", true, IResource.DEPTH_INFINITE);
for(CompilerError error:CompilerService.getErrorList(errors)){
IMarker marker = shaderFile.createMarker(IMarker.PROBLEM);
marker.setAttribute("CODE_KEY", "org.glsl.compiler.error");
marker.setAttribute("severity", IMarker.SEVERITY_ERROR);
marker.setAttribute("charStart", 1+nthOccurrence(shaderSource,'\n',error.line-2));
marker.setAttribute("charEnd", nthOccurrence(shaderSource,'\n',error.line-1));
marker.setAttribute("location", "line: "+(error.line-1)+" "+shaderFile.getLocation());
marker.setAttribute("lineNumber", error.line-1);
marker.setAttribute("message", error.text);
marker.setAttribute("URI_KEY", EcoreUtil.getURI(r.getContents().get(0)).toString());
marker.setAttribute("FIXABLE_KEY", true);
}
} catch (CoreException e1) {
e1.printStackTrace();
}
}
everything works fine, except for one thing: when i click on the error indicator next to the line in the editor ill get the infamous "no more handles" exception...
|
|
|
Powered by
FUDForum. Page generated in 0.02904 seconds