use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class ImportSelectionDialog method organizeImportsEdit.
/**
* Organize the imports according to the global preferences. If set,
* <ul>
* <li>Add imports necessary for missing references,</li>
* <li>Remove unused imports,</li>
* <li>Sort the import statements.</li>
* </ul>
* <p>
* These changes are not applied in the function, just collected in a
* <link>MultiTextEdit</link>, which is then returned.
* </p>
* TODO: notice and handle ambiguous references
*
* @param module
* The module which import statements are to organize.
* @param document
* The document that contains the module.
*
* @return The edit, which contains the proper changes.
*/
private static MultiTextEdit organizeImportsEdit(final TTCN3Module module, final IDocument document) throws BadLocationException {
final IProject prj = module.getProject();
final String doc = document.get();
final MultiTextEdit insertEdit = new MultiTextEdit();
final MultiTextEdit removeEdit = new MultiTextEdit();
final List<ImportText> newImports = new ArrayList<ImportText>();
final List<ImportText> importsKept = new ArrayList<ImportText>();
boolean needSorting = false;
if (addImports) {
// register the new needed imports
final Set<String> importNamesAdded = new HashSet<String>();
for (final Reference ref : module.getMissingReferences()) {
final Location missLoc = findReferenceInProject(ref, prj);
if (missLoc != null) {
final IFile file = (IFile) missLoc.getFile();
final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(file.getProject());
final Module addMod = parser.containedModule(file);
final String importName = addMod.getIdentifier().getTtcnName();
if (!importNamesAdded.contains(importName)) {
final StringBuilder impText = new StringBuilder("import from ").append(importName).append(" all;");
// if (importChangeMethod.equals(OrganizeImportPreferencePage.COMMENT_THEM)) {
impText.append(" // Added automatically to resolve ").append(ref.getDisplayName());
// }
newImports.add(new ImportText(importName, impText.toString() + NEWLINE));
importNamesAdded.add(importName);
if (reportDebug) {
final StringBuilder sb = new StringBuilder("For ").append(ref.getDisplayName()).append(": ");
sb.append(impText.toString());
TITANDebugConsole.println(sb.toString());
}
}
}
}
if (sortImports && !newImports.isEmpty()) {
needSorting = true;
}
}
if (!needSorting && sortImports) {
// are the imports already sorted ?
final List<ImportModule> oldImports = module.getImports();
for (int size = oldImports.size(), i = 0; i < size - 1 && !needSorting; i++) {
if (oldImports.get(i).getName().compareTo(oldImports.get(i + 1).getName()) > 0) {
needSorting = true;
}
if (oldImports.get(i).getLocation().getOffset() > oldImports.get(i + 1).getLocation().getOffset()) {
needSorting = true;
}
}
if (!needSorting && oldImports.size() > 1) {
// are the import strictly before the definitions ?
final int lastImportOffset = oldImports.get(oldImports.size() - 1).getLocation().getOffset();
final Definitions defs = module.getAssignmentsScope();
if (defs.getNofAssignments() > 0 && !oldImports.isEmpty()) {
for (int i = 0, size = defs.getNofAssignments(); i < size; ++i) {
final int temp = defs.getAssignmentByIndex(i).getLocation().getOffset();
if (temp < lastImportOffset) {
needSorting = true;
}
}
}
}
}
if (needSorting || removeImports) {
// remove the imports not needed, or every if sorting is required
for (final ImportModule m : module.getImports()) {
final Location delImp = m.getLocation();
final IRegion startLineRegion = document.getLineInformationOfOffset(delImp.getOffset());
final IRegion endLineRegion = document.getLineInformationOfOffset(delImp.getEndOffset());
final String delimeter = document.getLineDelimiter(document.getLineOfOffset(delImp.getEndOffset()));
final int delLength = delimeter == null ? 0 : delimeter.length();
if (needSorting || (removeImports && !m.getUsedForImportation())) {
if (reportDebug) {
final MessageConsoleStream stream = TITANDebugConsole.getConsole().newMessageStream();
TITANDebugConsole.println("Removing " + "'" + doc.substring(startLineRegion.getOffset(), endLineRegion.getOffset() + endLineRegion.getLength() + delLength) + "'", stream);
TITANDebugConsole.println("From " + startLineRegion.getOffset() + " till " + ((endLineRegion.getOffset() - startLineRegion.getOffset()) + endLineRegion.getLength() + delLength), stream);
}
/*if (importChangeMethod.equals(OrganizeImportPreferencePage.COMMENT_THEM)) {
removeEdit.addChild(new InsertEdit(m.getLocation().getOffset(), "/*"));
// hack to handle the semicolon
removeEdit.addChild(new InsertEdit(m.getLocation().getEndOffset() + 1, ""));
} else {*/
removeEdit.addChild(new DeleteEdit(startLineRegion.getOffset(), (endLineRegion.getOffset() - startLineRegion.getOffset()) + endLineRegion.getLength() + delLength));
// }
}
if (needSorting && (!removeImports || m.getUsedForImportation())) {
importsKept.add(new ImportText(m.getName(), doc.substring(startLineRegion.getOffset(), endLineRegion.getOffset() + endLineRegion.getLength() + delLength)));
}
}
}
if (!newImports.isEmpty() || (sortImports && needSorting)) {
// always insert at the beginning of the file
final int line = document.getLineOfOffset(module.getAssignmentsScope().getLocation().getOffset());
final IRegion lineRegion = document.getLineInformation(line);
final String delimeter = document.getLineDelimiter(line);
final int delimeterLength = delimeter == null ? 0 : delimeter.length();
final int startPos = lineRegion.getOffset() + lineRegion.getLength() + delimeterLength;
if (sortImports) {
if (needSorting || !newImports.isEmpty()) {
final List<ImportText> results = new ArrayList<ImportText>();
results.addAll(importsKept);
results.addAll(newImports);
Collections.sort(results);
for (final ImportText i : results) {
insertEdit.addChild(new InsertEdit(startPos, i.getText()));
}
}
} else {
Collections.sort(newImports);
for (final ImportText i : newImports) {
insertEdit.addChild(new InsertEdit(startPos, i.getText()));
}
}
}
final MultiTextEdit resultEdit = new MultiTextEdit();
if (insertEdit.hasChildren()) {
resultEdit.addChild(insertEdit);
}
if (removeEdit.hasChildren()) {
resultEdit.addChild(removeEdit);
}
return resultEdit;
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method calculateMultiDeclarationMoveContent.
/**
* Returns the content of an {@InsertEdit} to move a variable from a declaration list
*/
private String calculateMultiDeclarationMoveContent(final String fileContent, final StatementNode declStNode) {
final MultiDeclaration md = declStNode.getMultiDeclaration();
final StatementNode firstDeclPart = md.getFirstStatement();
final Definition firstDefInStmt = firstDeclPart.getDeclaredVar().getDefinition();
final Definition defVarToMove = declStNode.getDeclaredVar().getDefinition();
final Definition_Statement declStmt = (Definition_Statement) declStNode.getAstNode();
final Location declStmtLoc = declStmt.getLocation();
final String stmtContent = fileContent.substring(declStmtLoc.getOffset(), declStmtLoc.getEndOffset());
if (!stmtContent.contains(",")) {
ErrorReporter.logError("ChangeCreator.calculateMultiDeclarationMoveContent(): Given statement" + " is not a multi-declaration statement; loc: " + declStmtLoc.getOffset() + "-" + declStmtLoc.getEndOffset() + " in file " + declStmtLoc.getFile());
return null;
}
int prefixOffset;
int prefixEndOffset;
if (firstDefInStmt.equals(defVarToMove)) {
// first var to move
prefixOffset = findLineBeginningOffset(fileContent, declStmtLoc.getOffset());
prefixEndOffset = declStmtLoc.getOffset();
} else {
// not first var to move
prefixOffset = findLineBeginningOffset(fileContent, declStmtLoc.getOffset());
prefixEndOffset = firstDefInStmt.getIdentifier().getLocation().getOffset();
}
String prefixContent = fileContent.substring(prefixOffset, prefixEndOffset);
//
final int varOffset = defVarToMove.getLocation().getOffset();
final int varEndOffset = defVarToMove.getLocation().getEndOffset();
String varContent = fileContent.substring(varOffset, varEndOffset);
String suffixContent = "\n";
if (varContent.charAt(varContent.length() - 1) != ';') {
suffixContent = ";" + suffixContent;
}
// remove newlines from varContent
prefixContent = prefixContent.replaceAll("[\n\r]", " ");
varContent = varContent.replaceAll("[\n\r]", " ");
// System.err.println("mdcopyloc -->>>" + prefixContent + "<>" + varContent + "<>" + suffixContent + "<<<");
return prefixContent + varContent + suffixContent;
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method calculateMultiDeclarationCutLoc.
/**
* Returns the {@link Location} of the {@DeleteEdit} to remove a variable from a declaration list
*/
private Location calculateMultiDeclarationCutLoc(final String fileContent, final StatementNode declStNode) {
/*
* rules for removing multideclaration parts:
* if part is only one left: remove statement
* if part is first: remove trailing comma
* if part is last: remove leading comma
* if part is intermediate: remove trailing comma
* */
final MultiDeclaration md = declStNode.getMultiDeclaration();
final StatementNode firstDeclPart = md.getFirstStatement();
final Definition defVarToMove = declStNode.getDeclaredVar().getDefinition();
final Definition_Statement declStmt = (Definition_Statement) declStNode.getAstNode();
final boolean firstDefInMdMoved = firstDeclPart.isMoved();
final Location declStmtLoc = declStmt.getLocation();
final String stmtContent = fileContent.substring(declStmtLoc.getOffset(), declStmtLoc.getEndOffset());
if (!stmtContent.contains(",")) {
ErrorReporter.logError("ChangeCreator.calculateMultiDeclarationCutLoc(): Given statement" + " is not a multi-declaration statement; loc: " + declStmtLoc.getOffset() + "-" + declStmtLoc.getEndOffset() + " in file " + declStmtLoc.getFile());
return null;
}
//
if (md.getSize() <= 1) {
final Location cutLoc = findStatementLocation(fileContent, declStmt.getLocation(), true);
// System.err.println("mdcutloc <= 1 -->>>" + fileContent.substring(cutLoc.getOffset(), cutLoc.getEndOffset()) + "<<<");
return cutLoc;
}
//
int cutOffset = defVarToMove.getLocation().getOffset();
int cutEndOffset = defVarToMove.getLocation().getEndOffset();
if (md.isFirstStatement(declStNode)) {
// fist var
if (!md.isAllStatementsMoved()) {
cutOffset = defVarToMove.getIdentifier().getLocation().getOffset();
}
cutEndOffset = calculateEndOffsetIncludingTrailingComma(fileContent, cutEndOffset, declStmtLoc.getEndOffset());
} else if (md.isLastStatement(declStNode)) {
// last var
cutOffset = calculateOffsetIncludingLeadingComma(fileContent, cutOffset, declStmtLoc.getOffset());
} else {
// intermediate var
cutEndOffset = calculateEndOffsetIncludingTrailingComma(fileContent, cutEndOffset, declStmtLoc.getEndOffset());
}
// System.err.println("mdcutloc -->>>" + fileContent.substring(cutOffset, cutEndOffset) + "<<<");
return new Location(declStmtLoc.getFile(), declStmtLoc.getLine(), cutOffset, cutEndOffset);
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class StatementBlockContext method process_internal.
@Override
protected void process_internal() {
localVarIds = new ArrayList<Identifier>();
final Context bottom = getBottom();
final IVisitableNode bottomNode = bottom.getNode();
if (!(bottomNode instanceof Log_Statement)) {
ErrorReporter.logError("StatementBlockContext.process_internal(): Warning! Context chain bottom node is not a Log_Statement! ");
return;
}
final Log_Statement logst = (Log_Statement) bottomNode;
final Location logLoc = logst.getLocation();
//
final StatementBlock sb = getNode();
final StatementBlockVisitor vis = new StatementBlockVisitor(logLoc);
sb.accept(vis);
localVarIds.addAll(vis.getIdsFound());
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class DependencyCollector method reportOverlappingError.
private void reportOverlappingError(final ILocateableNode defCurrent, final ILocateableNode defOverlapping) {
final Location dLocOverlap = defOverlapping == null ? null : defOverlapping.getLocation();
final Location dLocCurr = defCurrent == null ? null : defCurrent.getLocation();
String idOverlap = null;
if (defOverlapping instanceof Definition) {
idOverlap = ((Definition) defOverlapping).getIdentifier().toString();
} else if (defOverlapping instanceof ImportModule) {
idOverlap = "ImportModule{" + ((ImportModule) defOverlapping).getIdentifier().toString() + "}";
} else if (defOverlapping instanceof FriendModule) {
idOverlap = "FriendModule{" + ((FriendModule) defOverlapping).getIdentifier().toString() + "}";
}
String idCurr = null;
if (defCurrent instanceof Definition) {
idCurr = ((Definition) defCurrent).getIdentifier().toString();
} else if (defCurrent instanceof ImportModule) {
idCurr = "ImportModule{" + ((ImportModule) defCurrent).getIdentifier().toString() + "}";
} else if (defCurrent instanceof FriendModule) {
idCurr = "FriendModule{" + ((FriendModule) defCurrent).getIdentifier().toString() + "}";
}
final String msg1 = (dLocCurr == null) ? "null" : "Definition id: " + idCurr + " (" + defCurrent.getClass().getSimpleName() + ") at " + dLocCurr.getFile() + ", offset " + dLocCurr.getOffset() + "-" + dLocCurr.getEndOffset();
final String msg2 = (dLocOverlap == null) ? "null" : "Definition id: " + idOverlap + " (" + defOverlapping.getClass().getSimpleName() + ") at " + dLocOverlap.getFile() + ", offset " + dLocOverlap.getOffset() + "-" + dLocOverlap.getEndOffset();
ErrorReporter.logError("Warning! Locations overlap while reading source project: \n" + msg1 + "\n WITH \n" + msg2);
}
Aggregations