Search in sources :

Example 6 with ImportModule

use of org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule 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;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) IFile(org.eclipse.core.resources.IFile) Reference(org.eclipse.titan.designer.AST.Reference) Definitions(org.eclipse.titan.designer.AST.TTCN3.definitions.Definitions) ArrayList(java.util.ArrayList) MessageConsoleStream(org.eclipse.ui.console.MessageConsoleStream) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IProject(org.eclipse.core.resources.IProject) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) IRegion(org.eclipse.jface.text.IRegion) Module(org.eclipse.titan.designer.AST.Module) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) HashSet(java.util.HashSet) Location(org.eclipse.titan.designer.AST.Location)

Example 7 with ImportModule

use of org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule 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);
}
Also used : FriendModule(org.eclipse.titan.designer.AST.TTCN3.definitions.FriendModule) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) Location(org.eclipse.titan.designer.AST.Location)

Example 8 with ImportModule

use of org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule in project titan.EclipsePlug-ins by eclipse.

the class DependencyCollector method readDependencies.

public WorkspaceJob readDependencies() {
    final WorkspaceJob job = new WorkspaceJob("ExtractModulePar: reading dependencies from source project") {

        @Override
        public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
            final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(sourceProj);
            // find all dependencies of the 'selection' definition
            Set<IResource> allFiles = new HashSet<IResource>();
            Set<IResource> asnFiles = new HashSet<IResource>();
            NavigableSet<ILocateableNode> dependencies = new TreeSet<ILocateableNode>(new LocationComparator());
            for (Def_ModulePar def : selection) {
                /*
					 * Def_ModulePars with mutliple entries in a single modulepar block have incorrect location info
					 *  (all entries have a location info equal to the location of their parent block)
					 *  that is why the dependencies must be collected into a set that is not sorted by location
					 *
					 *  TODO fix grammar definitions
					 * */
                Set<ILocateableNode> nonSortedTempSet = new HashSet<ILocateableNode>();
                collectDependencies(def, nonSortedTempSet, allFiles, asnFiles);
                dependencies.addAll(nonSortedTempSet);
                allFiles.add(def.getLocation().getFile());
                // adding the selection itself to the dependencies
                if (!dependencies.contains(def)) {
                    dependencies.add(def);
                }
                // get imports and friends for all files
                for (IResource r : allFiles) {
                    if (!(r instanceof IFile)) {
                        continue;
                    }
                    IFile f = (IFile) r;
                    Module m = projectSourceParser.containedModule(f);
                    ImportFinderVisitor impVisitor = new ImportFinderVisitor();
                    m.accept(impVisitor);
                    List<ImportModule> impDefs = impVisitor.getImportDefs();
                    List<FriendModule> friendDefs = impVisitor.getFriendDefs();
                    filterImportDefinitions(allFiles, impDefs, projectSourceParser);
                    filterFriendDefinitions(allFiles, friendDefs, projectSourceParser);
                    dependencies.addAll(impDefs);
                    dependencies.addAll(friendDefs);
                // the dependencies are sorted by file & location to avoid reading through a file multiple times
                }
            }
            // collect the text to insert into the new project
            copyMap = new HashMap<IPath, StringBuilder>();
            try {
                InputStream is = null;
                InputStreamReader isr = null;
                IResource lastFile = null;
                IResource currFile;
                ILocateableNode lastD = null;
                int currOffset = 0;
                for (ILocateableNode d : dependencies) {
                    currFile = d.getLocation().getFile();
                    if (!(currFile instanceof IFile)) {
                        ErrorReporter.logError("ExtractModulePar/DependencyCollector: IResource `" + currFile.getName() + "' is not an IFile.");
                        continue;
                    }
                    if (currFile != lastFile) {
                        // started reading new file
                        lastD = null;
                        if (lastFile != null) {
                            addToCopyMap(lastFile.getProjectRelativePath(), MODULE_TRAILER);
                        }
                        if (isr != null) {
                            isr.close();
                        }
                        if (is != null) {
                            is.close();
                        }
                        is = ((IFile) currFile).getContents();
                        isr = new InputStreamReader(is);
                        currOffset = 0;
                        Module m = projectSourceParser.containedModule((IFile) currFile);
                        String moduleName = m.getIdentifier().getTtcnName();
                        addToCopyMap(currFile.getProjectRelativePath(), MessageFormat.format(MODULE_HEADER, moduleName));
                    }
                    int toSkip = getNodeOffset(d) - currOffset;
                    if (toSkip < 0) {
                        reportOverlappingError(lastD, d);
                        continue;
                    }
                    isr.skip(toSkip);
                    // separators
                    if (d instanceof ImportModule && lastD instanceof ImportModule) {
                        addToCopyMap(currFile.getProjectRelativePath(), SINGLE_SEPARATOR);
                    } else if (d instanceof FriendModule && lastD instanceof FriendModule) {
                        addToCopyMap(currFile.getProjectRelativePath(), SINGLE_SEPARATOR);
                    } else {
                        addToCopyMap(currFile.getProjectRelativePath(), DOUBLE_SEPARATOR);
                    }
                    // 
                    insertDependency(d, currFile, isr);
                    currOffset = d.getLocation().getEndOffset();
                    lastFile = currFile;
                    lastD = d;
                }
                if (lastFile != null) {
                    addToCopyMap(lastFile.getProjectRelativePath(), MODULE_TRAILER);
                }
                if (isr != null) {
                    isr.close();
                }
                if (is != null) {
                    is.close();
                }
                // copy the full content of asn files without opening them
                filesToCopy = new ArrayList<IFile>();
                for (IResource r : asnFiles) {
                    if (!(r instanceof IFile)) {
                        ErrorReporter.logError("ExtractModulePar/DependencyCollector: IResource `" + r.getName() + "' is not an IFile.");
                        continue;
                    }
                    filesToCopy.add((IFile) r);
                }
            } catch (IOException ioe) {
                ErrorReporter.logError("ExtractModulePar/DependencyCollector: Error while reading source project.");
                return Status.CANCEL_STATUS;
            }
            return Status.OK_STATUS;
        }

        private void insertDependency(final ILocateableNode d, final IResource res, final InputStreamReader isr) throws IOException {
            char[] content = new char[d.getLocation().getEndOffset() - getNodeOffset(d)];
            isr.read(content, 0, content.length);
            addToCopyMap(res.getProjectRelativePath(), new String(content));
        }
    };
    job.setUser(true);
    job.schedule();
    return job;
}
Also used : IFile(org.eclipse.core.resources.IFile) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) FriendModule(org.eclipse.titan.designer.AST.TTCN3.definitions.FriendModule) IPath(org.eclipse.core.runtime.IPath) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Def_ModulePar(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) FriendModule(org.eclipse.titan.designer.AST.TTCN3.definitions.FriendModule) Module(org.eclipse.titan.designer.AST.Module) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) IResource(org.eclipse.core.resources.IResource)

Example 9 with ImportModule

use of org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule in project titan.EclipsePlug-ins by eclipse.

the class Definitions method updateSyntax.

/**
 * Handles the incremental parsing of this list of definitions.
 *
 * @param reparser
 *                the parser doing the incremental parsing.
 * @param importedModules
 *                the list of module importations found in the same
 *                module.
 * @param friendModules
 *                the list of friend module declaration in the same
 *                module.
 * @param controlpart
 *                the control part found in the same module.
 * @throws ReParseException
 *                 if there was an error while refreshing the location
 *                 information and it could not be solved internally.
 */
public void updateSyntax(final TTCN3ReparseUpdater reparser, final List<ImportModule> importedModules, final List<FriendModule> friendModules, final ControlPart controlpart) throws ReParseException {
    // calculate damaged region
    int result = 0;
    boolean enveloped = false;
    int nofDamaged = 0;
    int leftBoundary = location.getOffset();
    int rightBoundary = location.getEndOffset();
    final int damageOffset = reparser.getDamageStart();
    final int damageEndOffset = reparser.getDamageEnd();
    IAppendableSyntax lastAppendableBeforeChange = null;
    IAppendableSyntax lastPrependableBeforeChange = null;
    boolean isControlPossible = controlpart == null;
    if (controlpart != null) {
        final Location tempLocation = controlpart.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
        } else if (!reparser.isDamaged(tempLocation)) {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = controlpart;
            }
            if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = controlpart;
            }
        }
    }
    for (int i = 0, size = groups.size(); i < size && !enveloped; i++) {
        final Group tempGroup = groups.get(i);
        final Location tempLocation = tempGroup.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
            leftBoundary = tempLocation.getOffset();
            rightBoundary = tempLocation.getEndOffset();
        } else if (reparser.isDamaged(tempLocation)) {
            nofDamaged++;
        } else {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = tempGroup;
            }
            if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = tempGroup;
            }
        }
    }
    if (!groups.isEmpty()) {
        isControlPossible &= groups.get(groups.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    for (int i = 0, size = importedModules.size(); i < size && !enveloped; i++) {
        final ImportModule tempImport = importedModules.get(i);
        if (tempImport.getParentGroup() == null) {
            final Location tempLocation = tempImport.getLocation();
            if (reparser.envelopsDamage(tempLocation)) {
                enveloped = true;
                leftBoundary = tempLocation.getOffset();
                rightBoundary = tempLocation.getEndOffset();
            } else if (reparser.isDamaged(tempLocation)) {
                nofDamaged++;
            } else {
                if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                    leftBoundary = tempLocation.getEndOffset();
                    lastAppendableBeforeChange = tempImport;
                }
                if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                    rightBoundary = tempLocation.getOffset();
                    lastPrependableBeforeChange = tempImport;
                }
            }
        }
    }
    if (!importedModules.isEmpty()) {
        isControlPossible &= importedModules.get(importedModules.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    for (int i = 0, size = friendModules.size(); i < size && !enveloped; i++) {
        final FriendModule tempFriend = friendModules.get(i);
        if (tempFriend.getParentGroup() == null) {
            final Location tempLocation = tempFriend.getLocation();
            if (reparser.envelopsDamage(tempLocation)) {
                enveloped = true;
                leftBoundary = tempLocation.getOffset();
                rightBoundary = tempLocation.getEndOffset();
            } else if (reparser.isDamaged(tempLocation)) {
                nofDamaged++;
            } else {
                if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                    leftBoundary = tempLocation.getEndOffset();
                    lastAppendableBeforeChange = tempFriend;
                }
                if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                    rightBoundary = tempLocation.getOffset();
                    lastPrependableBeforeChange = tempFriend;
                }
            }
        }
    }
    if (!friendModules.isEmpty()) {
        isControlPossible &= friendModules.get(friendModules.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext() && !enveloped; ) {
        final Definition temp = iterator.next();
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            final Location cumulativeLocation = temp.getCumulativeDefinitionLocation();
            if (tempLocation.equals(cumulativeLocation) && reparser.envelopsDamage(cumulativeLocation)) {
                enveloped = true;
                leftBoundary = cumulativeLocation.getOffset();
                rightBoundary = cumulativeLocation.getEndOffset();
            } else if (reparser.isDamaged(cumulativeLocation)) {
                nofDamaged++;
                if (reparser.getDamageStart() == cumulativeLocation.getEndOffset()) {
                    lastAppendableBeforeChange = temp;
                } else if (reparser.getDamageEnd() == cumulativeLocation.getOffset()) {
                    lastPrependableBeforeChange = temp;
                }
            } else {
                if (cumulativeLocation.getEndOffset() < damageOffset && cumulativeLocation.getEndOffset() > leftBoundary) {
                    leftBoundary = cumulativeLocation.getEndOffset();
                    lastAppendableBeforeChange = temp;
                }
                if (cumulativeLocation.getOffset() > damageEndOffset && cumulativeLocation.getOffset() < rightBoundary) {
                    rightBoundary = cumulativeLocation.getOffset();
                    lastPrependableBeforeChange = temp;
                }
            }
            final Location tempCommentLocation = temp.getCommentLocation();
            if (tempCommentLocation != null && reparser.isDamaged(tempCommentLocation)) {
                nofDamaged++;
                rightBoundary = tempLocation.getEndOffset();
            }
        }
    }
    if (!definitions.isEmpty()) {
        isControlPossible &= definitions.get(definitions.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    // was not enveloped
    if (!enveloped && reparser.isDamaged(location)) {
        // the extension might be correct
        if (lastAppendableBeforeChange != null) {
            final boolean isBeingExtended = reparser.startsWithFollow(lastAppendableBeforeChange.getPossibleExtensionStarterTokens());
            if (isBeingExtended) {
                leftBoundary = lastAppendableBeforeChange.getLocation().getOffset();
                nofDamaged++;
                enveloped = false;
                reparser.extendDamagedRegion(leftBoundary, rightBoundary);
            }
        }
        if (lastPrependableBeforeChange != null) {
            final List<Integer> temp = lastPrependableBeforeChange.getPossiblePrefixTokens();
            if (temp != null && reparser.endsWithToken(temp)) {
                rightBoundary = lastPrependableBeforeChange.getLocation().getEndOffset();
                nofDamaged++;
                enveloped = false;
                reparser.extendDamagedRegion(leftBoundary, rightBoundary);
            }
        }
        if (nofDamaged != 0) {
            // remove damaged stuff
            removeStuffInRange(reparser, importedModules, friendModules);
            if (doubleDefinitions != null) {
                doubleDefinitions.clear();
            }
            lastUniquenessCheckTimeStamp = null;
            lastCompilationTimeStamp = null;
        }
        // extend damaged region till the neighbor definitions just here to avoid calculating something damaged or extended:
        // Perhaps it should be moved even farther:
        reparser.extendDamagedRegion(leftBoundary, rightBoundary);
    }
    // update what is left
    for (int i = 0; i < groups.size(); i++) {
        final Group temp = groups.get(i);
        final Location tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            try {
                temp.updateSyntax(reparser, importedModules, definitions, friendModules);
            } catch (ReParseException e) {
                if (e.getDepth() == 1) {
                    enveloped = false;
                    groups.remove(i);
                    i--;
                    reparser.extendDamagedRegion(tempLocation);
                    result = 1;
                } else {
                    if (doubleDefinitions != null) {
                        doubleDefinitions.clear();
                    }
                    lastUniquenessCheckTimeStamp = null;
                    e.decreaseDepth();
                    throw e;
                }
            }
        }
    }
    for (int i = 0; i < importedModules.size(); i++) {
        final ImportModule temp = importedModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                try {
                    final boolean isDamaged = enveloped && reparser.envelopsDamage(tempLocation);
                    temp.updateSyntax(reparser, enveloped && reparser.envelopsDamage(tempLocation));
                    if (isDamaged) {
                        ((TTCN3Module) parentScope).checkRoot();
                    }
                } catch (ReParseException e) {
                    if (e.getDepth() == 1) {
                        enveloped = false;
                        importedModules.remove(i);
                        i--;
                        reparser.extendDamagedRegion(tempLocation);
                        result = 1;
                    } else {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        e.decreaseDepth();
                        throw e;
                    }
                }
            }
        }
    }
    for (int i = 0; i < friendModules.size(); i++) {
        final FriendModule temp = friendModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                try {
                    final boolean isDamaged = enveloped && reparser.envelopsDamage(tempLocation);
                    temp.updateSyntax(reparser, enveloped && reparser.envelopsDamage(tempLocation));
                    if (isDamaged) {
                        ((TTCN3Module) parentScope).checkRoot();
                    }
                } catch (ReParseException e) {
                    if (e.getDepth() == 1) {
                        enveloped = false;
                        friendModules.remove(i);
                        i--;
                        reparser.extendDamagedRegion(tempLocation);
                        result = 1;
                    } else {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        e.decreaseDepth();
                        throw e;
                    }
                }
            }
        }
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext(); ) {
        final Definition temp = iterator.next();
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            final Location cumulativeLocation = temp.getCumulativeDefinitionLocation();
            if (reparser.isAffected(cumulativeLocation)) {
                try {
                    final boolean isDamaged = enveloped && reparser.envelopsDamage(tempLocation);
                    temp.updateSyntax(reparser, isDamaged);
                    if (reparser.getNameChanged()) {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        // to recheck the whole module
                        lastCompilationTimeStamp = null;
                        reparser.setNameChanged(false);
                    // This could also spread
                    }
                    if (isDamaged) {
                        // TODO lets move this into the definitions
                        temp.checkRoot();
                    }
                } catch (ReParseException e) {
                    if (e.getDepth() == 1) {
                        enveloped = false;
                        definitions.remove(temp);
                        reparser.extendDamagedRegion(cumulativeLocation);
                        result = 1;
                    } else {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        e.decreaseDepth();
                        throw e;
                    }
                }
            }
        }
    }
    if (result == 1) {
        removeStuffInRange(reparser, importedModules, friendModules);
        if (doubleDefinitions != null) {
            doubleDefinitions.clear();
        }
        lastUniquenessCheckTimeStamp = null;
        lastCompilationTimeStamp = null;
    }
    for (int i = 0, size = groups.size(); i < size; i++) {
        final Group temp = groups.get(i);
        final Location tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            reparser.updateLocation(tempLocation);
        }
    }
    for (int i = 0, size = importedModules.size(); i < size; i++) {
        final ImportModule temp = importedModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                reparser.updateLocation(tempLocation);
            }
        }
    }
    for (int i = 0, size = friendModules.size(); i < size; i++) {
        final FriendModule temp = friendModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                reparser.updateLocation(tempLocation);
            }
        }
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext(); ) {
        final Definition temp = iterator.next();
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            final Location cumulativeLocation = temp.getCumulativeDefinitionLocation();
            if (reparser.isAffected(tempLocation)) {
                if (tempLocation != cumulativeLocation) {
                    reparser.updateLocation(cumulativeLocation);
                }
                reparser.updateLocation(tempLocation);
            }
        }
    }
    final boolean tempIsControlPossible = isControlPossible;
    if (!enveloped) {
        if (reparser.envelopsDamage(location)) {
            reparser.extendDamagedRegion(leftBoundary, rightBoundary);
            result = reparse(reparser, tempIsControlPossible);
            result = Math.max(result - 1, 0);
            lastCompilationTimeStamp = null;
        } else {
            result = Math.max(result, 1);
        }
    }
    if (result == 0) {
        lastUniquenessCheckTimeStamp = null;
    } else {
        if (doubleDefinitions != null) {
            doubleDefinitions.clear();
        }
        lastUniquenessCheckTimeStamp = null;
        throw new ReParseException(result);
    }
}
Also used : IAppendableSyntax(org.eclipse.titan.designer.AST.TTCN3.IAppendableSyntax) ReParseException(org.eclipse.titan.designer.parsers.ttcn3parser.ReParseException) NULL_Location(org.eclipse.titan.designer.AST.NULL_Location) Location(org.eclipse.titan.designer.AST.Location)

Example 10 with ImportModule

use of org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule in project titan.EclipsePlug-ins by eclipse.

the class Group method updateSyntax.

/**
 * Handles the incremental parsing of this list of definitions.
 *
 * @param reparser
 *                the parser doing the incremental parsing.
 * @param allImportedModules
 *                the list of module importations found in the same
 *                module.
 * @param allDefinitions
 *                the list of definitions found in the same module.
 * @param allFriends
 *                the list of friend module declarations found in the
 *                same module.
 * @return in case of processing error the minimum amount of semantic
 *         levels that must be destroyed to handle the syntactic
 *         changes, otherwise 0.
 */
public void updateSyntax(final TTCN3ReparseUpdater reparser, final List<ImportModule> allImportedModules, final List<Definition> allDefinitions, final List<FriendModule> allFriends) throws ReParseException {
    int result = 0;
    Location tempLocation = identifier.getLocation();
    if (reparser.isDamaged(tempLocation)) {
        if (reparser.envelopsDamage(tempLocation) || reparser.isExtending(tempLocation)) {
            reparser.extendDamagedRegion(tempLocation);
            result = reparseIdentifier(reparser);
            if (result != 0) {
                throw new ReParseException(result);
            }
        } else {
            throw new ReParseException();
        }
    } else {
        reparser.updateLocation(tempLocation);
    }
    if (reparser.isDamaged(innerLocation) && !reparser.envelopsDamage(innerLocation)) {
        throw new ReParseException();
    }
    boolean enveloped = false;
    int nofDamaged = 0;
    int leftBoundary = innerLocation.getOffset();
    int rightBoundary = innerLocation.getEndOffset();
    final int damageOffset = reparser.getDamageStart();
    IAppendableSyntax lastAppendableBeforeChange = null;
    IAppendableSyntax lastPrependableBeforeChange = null;
    for (int i = 0, size = groups.size(); i < size && !enveloped; i++) {
        final Group temp = groups.get(i);
        tempLocation = temp.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
            leftBoundary = tempLocation.getOffset();
            rightBoundary = tempLocation.getEndOffset();
        } else if (reparser.isDamaged(tempLocation)) {
            nofDamaged++;
        } else {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = temp;
            }
            if (tempLocation.getOffset() >= damageOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = temp;
            }
        }
    }
    for (int i = 0, size = importedModules.size(); i < size && !enveloped; i++) {
        final ImportModule temp = importedModules.get(i);
        tempLocation = temp.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
            leftBoundary = tempLocation.getOffset();
            rightBoundary = tempLocation.getEndOffset();
        } else if (reparser.isDamaged(tempLocation)) {
            nofDamaged++;
        } else {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = temp;
            }
            if (tempLocation.getOffset() >= damageOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = temp;
            }
        }
    }
    for (int i = 0, size = friendModules.size(); i < size && !enveloped; i++) {
        final FriendModule temp = friendModules.get(i);
        tempLocation = temp.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
            leftBoundary = tempLocation.getOffset();
            rightBoundary = tempLocation.getEndOffset();
        } else if (reparser.isDamaged(tempLocation)) {
            nofDamaged++;
        } else {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = temp;
            }
            if (tempLocation.getOffset() >= damageOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = temp;
            }
        }
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext() && !enveloped; ) {
        final Definition temp = iterator.next();
        tempLocation = temp.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
            leftBoundary = tempLocation.getOffset();
            rightBoundary = tempLocation.getEndOffset();
        } else if (reparser.isDamaged(tempLocation)) {
            nofDamaged++;
            if (reparser.getDamageStart() == tempLocation.getEndOffset()) {
                lastAppendableBeforeChange = temp;
            } else if (reparser.getDamageEnd() == tempLocation.getOffset()) {
                lastPrependableBeforeChange = temp;
            }
        // reparser.extendDamagedRegion(temp_location);
        } else {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = temp;
            }
            if (tempLocation.getOffset() >= damageOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = temp;
            }
        }
        final Location tempCommentLocation = temp.getCommentLocation();
        if (tempCommentLocation != null && reparser.isDamaged(tempCommentLocation)) {
            rightBoundary = tempCommentLocation.getOffset();
            lastPrependableBeforeChange = temp;
        }
    }
    // was not enveloped
    if (!enveloped && reparser.envelopsDamage(location)) {
        reparser.extendDamagedRegion(leftBoundary, rightBoundary);
    }
    // correct
    if (lastAppendableBeforeChange != null) {
        final boolean isBeingExtended = reparser.startsWithFollow(lastAppendableBeforeChange.getPossibleExtensionStarterTokens());
        if (isBeingExtended) {
            leftBoundary = lastAppendableBeforeChange.getLocation().getOffset();
            nofDamaged++;
            enveloped = false;
            reparser.extendDamagedRegion(leftBoundary, rightBoundary);
        }
    }
    if (lastPrependableBeforeChange != null) {
        final List<Integer> temp = lastPrependableBeforeChange.getPossiblePrefixTokens();
        if (temp != null && reparser.endsWithToken(temp)) {
            rightBoundary = lastPrependableBeforeChange.getLocation().getEndOffset();
            nofDamaged++;
            enveloped = false;
            reparser.extendDamagedRegion(leftBoundary, rightBoundary);
        }
    }
    if (nofDamaged != 0) {
        // remove damaged stuff
        removeStuffInRange(reparser, allImportedModules, allDefinitions, friendModules);
    }
    // update what is left
    for (int i = 0; i < groups.size(); i++) {
        final Group temp = groups.get(i);
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            try {
                temp.updateSyntax(reparser, allImportedModules, allDefinitions, friendModules);
            } catch (ReParseException e) {
                if (e.getDepth() == 1) {
                    enveloped = false;
                    groups.remove(i);
                    i--;
                    reparser.extendDamagedRegion(tempLocation);
                    result = 1;
                } else {
                    e.decreaseDepth();
                    throw e;
                }
            }
        }
    }
    for (int i = 0; i < importedModules.size(); i++) {
        final ImportModule temp = importedModules.get(i);
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            try {
                temp.updateSyntax(reparser, enveloped && reparser.envelopsDamage(tempLocation));
            } catch (ReParseException e) {
                if (e.getDepth() == 1) {
                    enveloped = false;
                    importedModules.remove(i);
                    i--;
                    reparser.extendDamagedRegion(tempLocation);
                    result = 1;
                } else {
                    e.decreaseDepth();
                    throw e;
                }
            }
        }
    }
    for (int i = 0; i < friendModules.size(); i++) {
        final FriendModule temp = friendModules.get(i);
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            try {
                temp.updateSyntax(reparser, enveloped && reparser.envelopsDamage(tempLocation));
            } catch (ReParseException e) {
                if (e.getDepth() == 1) {
                    enveloped = false;
                    friendModules.remove(i);
                    i--;
                    reparser.extendDamagedRegion(tempLocation);
                    result = 1;
                } else {
                    e.decreaseDepth();
                    throw e;
                }
            }
        }
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext(); ) {
        final Definition temp = iterator.next();
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            try {
                final boolean isDamaged = enveloped && reparser.envelopsDamage(tempLocation);
                temp.updateSyntax(reparser, isDamaged);
                if (reparser.getNameChanged()) {
                    lastUniquenessCheckTimeStamp = null;
                    reparser.setNameChanged(false);
                }
                if (isDamaged) {
                    temp.checkRoot();
                }
            } catch (ReParseException e) {
                if (e.getDepth() == 1) {
                    enveloped = false;
                    definitions.remove(temp);
                    reparser.extendDamagedRegion(tempLocation);
                    result = 1;
                } else {
                    e.decreaseDepth();
                    throw e;
                }
            }
        }
    }
    if (result == 1) {
        removeStuffInRange(reparser, allImportedModules, allDefinitions, friendModules);
    }
    for (int i = 0, size = groups.size(); i < size; i++) {
        final Group temp = groups.get(i);
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            reparser.updateLocation(tempLocation);
        }
    }
    for (int i = 0, size = importedModules.size(); i < size; i++) {
        final ImportModule temp = importedModules.get(i);
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            reparser.updateLocation(tempLocation);
        }
    }
    for (int i = 0, size = friendModules.size(); i < size; i++) {
        final FriendModule temp = friendModules.get(i);
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            reparser.updateLocation(tempLocation);
        }
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext(); ) {
        final Definition temp = iterator.next();
        tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            reparser.updateLocation(tempLocation);
        }
    }
    if (withAttributesPath != null && reparser.isAffected(withAttributesPath.getLocation())) {
        if (reparser.envelopsDamage(withAttributesPath.getLocation())) {
            reparser.extendDamagedRegion(withAttributesPath.getLocation());
            result = reparseOptionalWithStatement(reparser);
            if (result != 0) {
                throw new ReParseException(result);
            }
            return;
        }
        withAttributesPath.updateSyntax(reparser, reparser.envelopsDamage(withAttributesPath.getLocation()));
        reparser.updateLocation(withAttributesPath.getLocation());
    }
    if (!enveloped && reparser.envelopsDamage(innerLocation)) {
        reparser.extendDamagedRegion(leftBoundary, rightBoundary);
        result = reparseModuleDefinitionsList(reparser);
    }
    reparser.updateLocation(innerLocation);
    if (result > 1) {
        throw new ReParseException(result - 1);
    }
    return;
}
Also used : IAppendableSyntax(org.eclipse.titan.designer.AST.TTCN3.IAppendableSyntax) ReParseException(org.eclipse.titan.designer.parsers.ttcn3parser.ReParseException) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

ImportModule (org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule)9 ArrayList (java.util.ArrayList)7 Module (org.eclipse.titan.designer.AST.Module)7 Location (org.eclipse.titan.designer.AST.Location)6 HashSet (java.util.HashSet)5 IFile (org.eclipse.core.resources.IFile)5 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)5 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)5 TreeSet (java.util.TreeSet)3 FriendModule (org.eclipse.titan.designer.AST.TTCN3.definitions.FriendModule)3 MessageConsoleStream (org.eclipse.ui.console.MessageConsoleStream)3 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 IRegion (org.eclipse.jface.text.IRegion)2 DeleteEdit (org.eclipse.text.edits.DeleteEdit)2 InsertEdit (org.eclipse.text.edits.InsertEdit)2 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)2 Reference (org.eclipse.titan.designer.AST.Reference)2 IAppendableSyntax (org.eclipse.titan.designer.AST.TTCN3.IAppendableSyntax)2 Definitions (org.eclipse.titan.designer.AST.TTCN3.definitions.Definitions)2