document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(), replacementString);
return;
}
}
// we could create an import statement if there is no conflict
Model file = (Model) context.getContents().get(0);
ComponentDefinition clazz = file.getComponentDef();
QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(typeName);
if (qualifiedName.getSegmentCount() == 1) {
// type resides in default package - no need to hassle with imports
proposal.setCursorPosition(proposalReplacementString.length());
document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(), proposalReplacementString);
return;
}
IEObjectDescription description = scope.getSingleElement(qualifiedName.skipFirst(qualifiedName.getSegmentCount() - 1));
if (description != null) {
// there exists a conflict - insert fully qualified name
proposal.setCursorPosition(proposalReplacementString.length());
document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(), proposalReplacementString);
return;
}
String fqnName = importConverter.toString(typeName);
boolean found = false;
for( Import i : file.getImports() ) {
if( i.getImportedNamespace().equals(fqnName) ) {
found = true;
}
}
// Import does not introduce ambiguities - add import and insert short name
String shortName = qualifiedName.getLastSegment();
int topPixel = -1;
// store the pixel coordinates to prevent the ui from flickering
StyledText widget = viewer.getTextWidget();
if (widget != null)
topPixel = widget.getTopPixel();
ITextViewerExtension viewerExtension = null;
if (viewer instanceof ITextViewerExtension) {
viewerExtension = (ITextViewerExtension) viewer;
viewerExtension.setRedraw(false);
}
DocumentRewriteSession rewriteSession = null;
try {
if (document instanceof IDocumentExtension4) {
rewriteSession = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED_SMALL);
}
// compute import statement's offset
int offset = 0;
boolean startWithLineBreak = true;
boolean endWithLineBreak = false;
if (file.getImports().isEmpty()) {
startWithLineBreak = false;
if (clazz == null) {
offset = document.getLength();
} else {
ICompositeNode node = NodeModelUtils.getNode(clazz);
offset = node.getOffset();
endWithLineBreak = true;
}
} else {
ICompositeNode node = NodeModelUtils.getNode(file.getImports().get(file.getImports().size() - 1));
offset = node.getOffset() + node.getLength();
}
offset = Math.min(proposal.getReplacementOffset(), offset);
// apply short proposal