Bug 411238 - SourceModuleDocumentProvider does not support DefaultProblem with only "line" position.
Summary: SourceModuleDocumentProvider does not support DefaultProblem with only "line"...
Status: NEW
Alias: None
Product: DLTK
Classification: Technology
Component: Common (show other bugs)
Version: 4.0   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 5.2   Edit
Assignee: dltk.common-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-06-20 08:45 EDT by Simon Bernard CLA
Modified: 2015-04-17 12:14 EDT (History)
2 users (show)

See Also:


Attachments
double marker (32.73 KB, image/png)
2015-04-17 05:38 EDT, Kaloyan Raev CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Bernard CLA 2013-06-20 08:45:34 EDT
When you parse code and  build a ModuleDeclaration (AST node) you could add DefaultProblem to IProblemReporter.

If this DefaultProblem has just a line position and no offsetposition, The SourceModuleDocumentProvider does not manage it. (so the editor error annotation is not correctly positionned)

The problem is in org.eclipse.dltk.internal.ui.editor.SourceModuleDocumentProvider.SourceModuleAnnotationModel.createPositionFromProblem(IProblem).

a fix could be :
----------------
protected Position createPositionFromProblem(IProblem problem) {
  int start = problem.getSourceStart();
  int end = problem.getSourceEnd();
  
  if (start > end) {
  	end = start + end;
  	start = end - start;
  	end = end - start;
  }
  
  if (start == -1 && end == -1) {
  	// marker line number is 1-based
  	int line = problem.getSourceLineNumber();
  	if (line > 0 && fDocument != null) {
  		try {
  			start = fDocument.getLineOffset(line - 1);
  			end = start;
  		} catch (BadLocationException x) {
  		}
  	}
  }
  
  if (start > -1 && end > -1)
  	return new Position(start, end - start);
  
  return null;
}



Inspired by org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel.createPositionFromMarker(IMarker)

I would like to implement a workarround by extending the SourceModuleDocumentProvider but it's impossible because this is a singleton which is used everywhere ...
Comment 1 Simon Bernard CLA 2014-11-18 10:05:08 EST
pushed on gerrit : https://git.eclipse.org/r/#/c/36413/
Comment 2 Kaloyan Raev CLA 2015-04-16 05:57:36 EDT
Simon, can you tell me how I can have a DefaultProblem with only line position? So I can test your patch.
Comment 3 Simon Bernard CLA 2015-04-16 09:19:59 EDT
On AbstractSourceParser.parse, you create a DefaultProblem with just line position (no offset) and marker will be not placed at the right line in editor.


If you have the code of LDT, you could just remove the workarround in LuaSourceParser line 160 

// -- TODO ECLIPSE 411238
// -- we must calculate offset because DLTK does not support 'line' positioning
if (problem.getSourceEnd() < 0) {
  try {
    final int line = problem.getSourceLineNumber();
    final Document document = new Document(source);
    int endLineOffset = document.getLineOffset(line) + document.getLineLength(line) - 1;
    problem.setSourceStart(fixer.getCharacterPosition(problem.getSourceStart()));
    problem.setSourceEnd(endLineOffset);
  } catch (BadLocationException e) {
    Activator.logWarning("Unable to retrive error offset", e); //$NON-NLS-1$
  }
} else {
  // Handle encoding shifts
  problem.setSourceStart(fixer.getCharacterPosition(problem.getSourceStart()));
  problem.setSourceEnd(fixer.getCharacterPosition(problem.getSourceEnd()));
}

As it was said in the gerrit review, the code seems a bit strange, it was inspired from org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel.createPositionFromMarker(IMarker) and maybe it would be rewrite in clearer way.
Comment 4 Kaloyan Raev CLA 2015-04-17 05:38:00 EDT
(In reply to Simon Bernard from comment #3)
> On AbstractSourceParser.parse, you create a DefaultProblem with just line
> position (no offset) and marker will be not placed at the right line in
> editor.

I don't really understand how to create the default problem.

However I've tried the remove the workaround from LuaSourceParser and apply the suggested patch in DLTK. In this case I get a double marker. I am not sure that this is exactly the effect you are looking for. I will attach screenshot.
Comment 5 Kaloyan Raev CLA 2015-04-17 05:38:30 EDT
Created attachment 252482 [details]
double marker
Comment 6 Alex Panchenko CLA 2015-04-17 06:03:50 EDT
The editor merges resource markers and temporary problems from editor contents.
If marker and temporary problem cover the same range (and have the same message), then 1 marker is displayed.
So, my guess in this case they are not treated identical.
Comment 7 Alex Panchenko CLA 2015-04-17 06:04:48 EDT
* 1 problem annotation is displayed.
Comment 8 Simon Bernard CLA 2015-04-17 12:14:36 EDT
(In reply to Kaloyan Raev from comment #4)
> I don't really understand how to create the default problem.
I mean by modifying the code (hardcoding) but removing the workaround works well too.

For the duplicate marker problem, it's clearly not what it was expected :p.

If you remove the workaround and you sysout the line in error line 130:
String cleanedSource = sourceValidator.getCleanedSource();
System.out.println(sourceValidator.getLineIndex());
if (!valid)
  module.setProblem(sourceValidator.getLineIndex(), -1, -1, -1, sourceValidator.getErrorMessage());

You should see that the annotation is at the right line.
But not the marker which is at line n+1.

in org.eclipse.dltk.compiler.problem.ProblemCollector.createMarkers(IResource, IProblemFactory, IProblemSeverityTranslator)
I see LINE_NUMBER attibutes is set with problem.getSourceLineNumber() + 1.
This seems strange...
If I remove the +1 there no more duplicate annotation.
(but remove a +1 like that is a bit creepy oO)