Search in sources :

Example 1 with ReferenceFinder

use of org.eclipse.titan.designer.AST.ReferenceFinder in project titan.EclipsePlug-ins by eclipse.

the class OccurencesMarker method findOccurrencesReferenceBased.

/**
 * Finds the occurrences of the element located on the given offset.
 * This solution can be used, when the locations are not correct. (e.g.
 * in case of an ASN.1 file)
 *
 * @param document
 * @param reference
 * @param module
 *                The module to search the occurrences in
 * @param offset
 *                An offset in the module
 * @return The found references. Includes the definition of the element.
 */
protected List<Hit> findOccurrencesReferenceBased(final IDocument document, final Reference reference, final Module module, final int offset) {
    Scope scope = module.getSmallestEnclosingScope(offset);
    if (scope == null) {
        removeOccurences(false);
        error(document, offset, "Can not determine the smallest enclosing scope.");
        return new ArrayList<ReferenceFinder.Hit>();
    }
    reference.setMyScope(scope);
    if (reference.getId() == null) {
        removeOccurences(false);
        error(document, offset, "The identifier of the reference is null.");
        return new ArrayList<ReferenceFinder.Hit>();
    }
    if (reference.getSubreferences().size() > 1) {
        // highlighting the subreferences is not yet supported
        removeOccurences(false);
        return new ArrayList<ReferenceFinder.Hit>();
    }
    ReferenceFinder referenceFinder;
    List<Hit> result = null;
    boolean found = false;
    if (scope.hasAssignmentWithId(CompilationTimeStamp.getBaseTimestamp(), reference.getId()) || (scope.getModuleScope().hasImportedAssignmentWithID(CompilationTimeStamp.getBaseTimestamp(), reference.getId()))) {
        Assignment assignment = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
        if (assignment == null) {
            error(document, offset, "The assignment could not be determined from the reference: " + reference.getDisplayName());
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        if (!assignment.shouldMarkOccurrences()) {
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        try {
            referenceFinder = new ReferenceFinder(assignment);
        } catch (final IllegalArgumentException e) {
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        result = referenceFinder.findReferencesInModule(module);
        // Hack to eliminate false positive results
        if (assignment.getLocation().containsOffset(offset)) {
            found = true;
        } else {
            for (Hit hit : result) {
                if (hit.identifier.getLocation().containsOffset(offset)) {
                    found = true;
                    break;
                }
            }
        }
        if (found && assignment.getMyScope().getModuleScope() == module && assignment.getIdentifier() != null) {
            result.add(new Hit(assignment.getIdentifier()));
        }
    }
    if (!found) {
        // Check if the reference points to a field of a type
        // definition
        referenceFinder = new ReferenceFinder();
        referenceFinder.detectAssignmentDataByOffset(module, offset, editor, false, false);
        Assignment assignment = referenceFinder.assignment;
        if (assignment == null) {
            removeOccurences(false);
            error(document, offset, "Could not detect the assignment.");
            return new ArrayList<ReferenceFinder.Hit>();
        }
        if (assignment.getAssignmentType() != null && assignment.getAssignmentType() != Assignment_type.A_TYPE || referenceFinder.fieldId == null || !assignment.shouldMarkOccurrences()) {
            removeOccurences(false);
            return new ArrayList<ReferenceFinder.Hit>();
        }
        result = referenceFinder.findReferencesInModule(module);
        if (referenceFinder.fieldId != null) {
            result.add(new Hit(referenceFinder.fieldId));
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) Hit(org.eclipse.titan.designer.AST.ReferenceFinder.Hit) Scope(org.eclipse.titan.designer.AST.Scope) ReferenceFinder(org.eclipse.titan.designer.AST.ReferenceFinder) ArrayList(java.util.ArrayList)

Example 2 with ReferenceFinder

use of org.eclipse.titan.designer.AST.ReferenceFinder in project titan.EclipsePlug-ins by eclipse.

the class ReferenceSearch method runAction.

/**
 * Helper function used by FindReferences classes for TTCN-3, ASN.1 and
 * TTCNPP editors
 */
public static void runAction(final IEditorPart targetEditor, final ISelection selection) {
    targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(null);
    IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
    if (file == null) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(FILENOTIDENTIFIABLE);
        return;
    }
    if (!TITANNature.hasTITANNature(file.getProject())) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(TITANNature.NO_TITAN_FILE_NATURE_FOUND);
        return;
    }
    IPreferencesService prefs = Platform.getPreferencesService();
    boolean reportDebugInformation = prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null);
    int offset;
    if (selection instanceof TextSelection && !selection.isEmpty() && !"".equals(((TextSelection) selection).getText())) {
        if (reportDebugInformation) {
            TITANDebugConsole.println("text selected: " + ((TextSelection) selection).getText());
        }
        TextSelection tSelection = (TextSelection) selection;
        offset = tSelection.getOffset() + tSelection.getLength();
    } else {
        offset = ((IEditorWithCarretOffset) targetEditor).getCarretOffset();
    }
    // find the module
    ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
    if (ResourceExclusionHelper.isExcluded(file)) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(MessageFormat.format(EXCLUDEDFROMBUILD, file.getFullPath()));
        return;
    }
    final Module module = projectSourceParser.containedModule(file);
    if (module == null) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(MessageFormat.format(NOTFOUNDMODULE, file.getName()));
        return;
    }
    final ReferenceFinder rf = new ReferenceFinder();
    boolean isDetected = rf.detectAssignmentDataByOffset(module, offset, targetEditor, true, reportDebugInformation);
    if (!isDetected) {
        return;
    }
    final ReferenceSearchQuery query = new ReferenceSearchQuery(rf, module, file.getProject());
    for (ISearchQuery runningQuery : NewSearchUI.getQueries()) {
        NewSearchUI.cancelQuery(runningQuery);
    }
    NewSearchUI.runQueryInBackground(query);
}
Also used : IFile(org.eclipse.core.resources.IFile) ReferenceFinder(org.eclipse.titan.designer.AST.ReferenceFinder) TextSelection(org.eclipse.jface.text.TextSelection) Module(org.eclipse.titan.designer.AST.Module) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) ISearchQuery(org.eclipse.search.ui.ISearchQuery)

Example 3 with ReferenceFinder

use of org.eclipse.titan.designer.AST.ReferenceFinder in project titan.EclipsePlug-ins by eclipse.

the class DefinitionDeclaration method getReferences.

@Override
public List<Hit> getReferences(final Module module) {
    try {
        final ReferenceFinder referenceFinder = new ReferenceFinder(ass);
        final List<Hit> result = referenceFinder.findReferencesInModule(module);
        if (ass.getMyScope().getModuleScope() == module) {
            result.add(new Hit(ass.getIdentifier()));
        }
        return result;
    } catch (final IllegalArgumentException e) {
        return new ArrayList<ReferenceFinder.Hit>();
    }
}
Also used : Hit(org.eclipse.titan.designer.AST.ReferenceFinder.Hit) ReferenceFinder(org.eclipse.titan.designer.AST.ReferenceFinder)

Example 4 with ReferenceFinder

use of org.eclipse.titan.designer.AST.ReferenceFinder in project titan.EclipsePlug-ins by eclipse.

the class FieldDeclaration method getReferences.

@Override
public List<Hit> getReferences(final Module module) {
    try {
        final ReferenceFinder referenceFinder = new ReferenceFinder(ass);
        referenceFinder.fieldId = fieldId;
        final List<Hit> result = referenceFinder.findReferencesInModule(module);
        if (ass.getMyScope().getModuleScope() == module) {
            result.add(new Hit(fieldId));
        }
        return result;
    } catch (final IllegalArgumentException e) {
        return new ArrayList<ReferenceFinder.Hit>();
    }
}
Also used : Hit(org.eclipse.titan.designer.AST.ReferenceFinder.Hit) ReferenceFinder(org.eclipse.titan.designer.AST.ReferenceFinder)

Example 5 with ReferenceFinder

use of org.eclipse.titan.designer.AST.ReferenceFinder in project titan.EclipsePlug-ins by eclipse.

the class RenameRefactoring method runAction.

/**
 * Helper function used by RenameRefactoringAction classes for TTCN-3,
 * ASN.1 and TTCNPP editors
 */
public static void runAction(final IEditorPart targetEditor, final ISelection selection) {
    final IStatusLineManager statusLineManager = targetEditor.getEditorSite().getActionBars().getStatusLineManager();
    statusLineManager.setErrorMessage(null);
    final IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
    if (file == null) {
        statusLineManager.setErrorMessage(FILENOTIDENTIFIABLE);
        return;
    }
    if (!TITANNature.hasTITANNature(file.getProject())) {
        statusLineManager.setErrorMessage(TITANNature.NO_TITAN_FILE_NATURE_FOUND);
        return;
    }
    final IPreferencesService prefs = Platform.getPreferencesService();
    final boolean reportDebugInformation = prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null);
    int offset;
    if (selection instanceof TextSelection && !selection.isEmpty() && !"".equals(((TextSelection) selection).getText())) {
        if (reportDebugInformation) {
            TITANDebugConsole.getConsole().newMessageStream().println("text selected: " + ((TextSelection) selection).getText());
        }
        TextSelection tSelection = (TextSelection) selection;
        offset = tSelection.getOffset() + tSelection.getLength();
    } else {
        offset = ((IEditorWithCarretOffset) targetEditor).getCarretOffset();
    }
    // run semantic analysis to have up-to-date AST
    // FIXME: it does not work for incremental parsing
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
    final WorkspaceJob job = projectSourceParser.analyzeAll();
    if (job == null) {
        if (reportDebugInformation) {
            TITANDebugConsole.getConsole().newMessageStream().println("Rename refactoring: WorkspaceJob to analyze project could not be created.");
        }
        return;
    }
    try {
        job.join();
    } catch (InterruptedException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return;
    }
    // find the module
    if (ResourceExclusionHelper.isExcluded(file)) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(MessageFormat.format(EXCLUDEDFROMBUILD, file.getFullPath()));
        return;
    }
    final Module module = projectSourceParser.containedModule(file);
    if (module == null) {
        statusLineManager.setErrorMessage(MessageFormat.format(NOTFOUNDMODULE, file.getName()));
        return;
    }
    ReferenceFinder rf = findOccurrencesLocationBased(module, offset);
    if (rf == null) {
        rf = new ReferenceFinder();
        boolean isDetected = rf.detectAssignmentDataByOffset(module, offset, targetEditor, true, reportDebugInformation);
        if (!isDetected) {
            return;
        }
    }
    RenameRefactoring renameRefactoring = new RenameRefactoring(file, module, rf);
    RenameRefactoringWizard renameWizard = new RenameRefactoringWizard(renameRefactoring);
    RefactoringWizardOpenOperation operation = new RefactoringWizardOpenOperation(renameWizard);
    try {
        operation.run(targetEditor.getEditorSite().getShell(), "");
    } catch (InterruptedException irex) {
        // operation was canceled
        if (reportDebugInformation) {
            TITANDebugConsole.getConsole().newMessageStream().println("Rename refactoring has been cancelled");
        }
    } finally {
        // ===================================
        // === Re-analysis after renaming ====
        // ===================================
        Map<Module, List<Hit>> changed = rf.findAllReferences(module, file.getProject(), null, reportDebugInformation);
        final Set<Module> modules = new HashSet<Module>();
        modules.add(module);
        modules.addAll(changed.keySet());
        reanalyseAstAfterRefactoring(file.getProject(), modules);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IStatusLineManager(org.eclipse.jface.action.IStatusLineManager) TextSelection(org.eclipse.jface.text.TextSelection) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) RefactoringWizardOpenOperation(org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation) ReferenceFinder(org.eclipse.titan.designer.AST.ReferenceFinder) List(java.util.List) ArrayList(java.util.ArrayList) Module(org.eclipse.titan.designer.AST.Module) ASN1Module(org.eclipse.titan.designer.AST.ASN1.definitions.ASN1Module) HashSet(java.util.HashSet)

Aggregations

ReferenceFinder (org.eclipse.titan.designer.AST.ReferenceFinder)5 Hit (org.eclipse.titan.designer.AST.ReferenceFinder.Hit)3 ArrayList (java.util.ArrayList)2 IFile (org.eclipse.core.resources.IFile)2 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)2 TextSelection (org.eclipse.jface.text.TextSelection)2 Module (org.eclipse.titan.designer.AST.Module)2 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)2 HashSet (java.util.HashSet)1 List (java.util.List)1 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)1 IStatusLineManager (org.eclipse.jface.action.IStatusLineManager)1 RefactoringWizardOpenOperation (org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation)1 ISearchQuery (org.eclipse.search.ui.ISearchQuery)1 ASN1Module (org.eclipse.titan.designer.AST.ASN1.definitions.ASN1Module)1 Assignment (org.eclipse.titan.designer.AST.Assignment)1 Scope (org.eclipse.titan.designer.AST.Scope)1