Search in sources :

Example 66 with Module

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

the class OpenDeclaration method doOpenDeclaration.

private final void doOpenDeclaration() {
    if (targetEditor == null || !(targetEditor instanceof TTCN3Editor)) {
        return;
    }
    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;
    }
    if (ResourceExclusionHelper.isExcluded(file)) {
        MessageDialog.openError(null, "Open Declaration does not work within excluded resources", "This module is excluded from build. To use the Open Declaration " + "feature please click on the 'Toggle exclude from build state' in the context menu of the Project Explorer. ");
        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 = ((TTCN3Editor) targetEditor).getCarretOffset();
    }
    ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
    final Module module = projectSourceParser.containedModule(file);
    if (module == null) {
        if (reportDebugInformation) {
            TITANDebugConsole.println("The file " + file.getLocation() + "does not seem to contain a valid module.");
            CompilationTimeStamp timestamp = projectSourceParser.getLastTimeChecked();
            if (timestamp == null) {
                TITANDebugConsole.println("The project " + file.getProject() + " was not yet analyzed semantically");
            } else {
                TITANDebugConsole.println("The project " + file.getProject() + " was last checked in " + projectSourceParser.getLastTimeChecked().toString());
            }
        }
        return;
    }
    IdentifierFinderVisitor visitor = new IdentifierFinderVisitor(offset);
    module.accept(visitor);
    final Declaration decl = visitor.getReferencedDeclaration();
    if (decl == null) {
        if (reportDebugInformation) {
            TITANDebugConsole.println("No visible elements found");
        }
        return;
    }
    selectAndRevealDeclaration(decl.getIdentifier().getLocation());
    return;
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) IFile(org.eclipse.core.resources.IFile) IdentifierFinderVisitor(org.eclipse.titan.designer.declarationsearch.IdentifierFinderVisitor) TextSelection(org.eclipse.jface.text.TextSelection) CompilationTimeStamp(org.eclipse.titan.designer.parsers.CompilationTimeStamp) Declaration(org.eclipse.titan.designer.declarationsearch.Declaration) Module(org.eclipse.titan.designer.AST.Module) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Example 67 with Module

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

the class AddImport method run.

@Override
public void run(final IAction action) {
    TITANDebugConsole.println("Add import called: ");
    if (targetEditor == null || !(targetEditor instanceof TTCN3Editor)) {
        return;
    }
    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.isEmpty() && selection instanceof TextSelection && !"".equals(((TextSelection) selection).getText())) {
        if (reportDebugInformation) {
            TITANDebugConsole.println("text selected: " + ((TextSelection) selection).getText());
        }
        TextSelection tSelection = (TextSelection) selection;
        offset = tSelection.getOffset() + tSelection.getLength();
    } else {
        offset = ((TTCN3Editor) targetEditor).getCarretOffset();
    }
    DeclarationCollector declarationCollector = OpenDeclarationHelper.findVisibleDeclarations(targetEditor, new TTCN3ReferenceParser(false), ((TTCN3Editor) targetEditor).getDocument(), offset, false);
    if (declarationCollector == null) {
        return;
    }
    List<DeclarationCollectionHelper> collected = declarationCollector.getCollected();
    if (collected.isEmpty()) {
        // FIXME add semantic check guard on project level.
        ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
        if (reportDebugInformation) {
            TITANDebugConsole.println("No visible elements found");
        }
        for (String moduleName2 : projectSourceParser.getKnownModuleNames()) {
            Module module2 = projectSourceParser.getModuleByName(moduleName2);
            if (module2 != null) {
                // Visit each file in the project one by
                // one instead of
                // "module2.getAssignments().addDeclaration(declarationCollector)".
                Assignments assignments = module2.getAssignments();
                for (int i = 0; i < assignments.getNofAssignments(); i++) {
                    assignments.getAssignmentByIndex(i).addDeclaration(declarationCollector, 0);
                }
            }
        }
        if (declarationCollector.getCollectionSize() == 0) {
            targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(NOTTTCN3DECLARATION);
            return;
        }
        if (reportDebugInformation) {
            TITANDebugConsole.println("Elements were only found in not visible modules");
        }
        DeclarationCollectionHelper resultToInsert = null;
        if (collected.size() == 1) {
            resultToInsert = collected.get(0);
        } else {
            OpenDeclarationLabelProvider labelProvider = new OpenDeclarationLabelProvider();
            ElementListSelectionDialog dialog = new ElementListSelectionDialog(null, labelProvider);
            dialog.setTitle("Add Import");
            dialog.setMessage("Choose element to generate an import statement for.");
            dialog.setElements(collected.toArray());
            if (dialog.open() == Window.OK) {
                if (reportDebugInformation) {
                    TITANDebugConsole.getConsole().newMessageStream().println("Selected: " + dialog.getFirstResult());
                }
                resultToInsert = (DeclarationCollectionHelper) dialog.getFirstResult();
            }
        }
        if (resultToInsert == null) {
            return;
        }
        IFile newfile = (IFile) resultToInsert.location.getFile();
        Module newModule = projectSourceParser.containedModule(newfile);
        if (newModule == null) {
            targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage("Could not identify the module in file " + newfile.getName());
            return;
        }
        String ttcnName = newModule.getIdentifier().getTtcnName();
        TITANDebugConsole.println("the new module to insert: " + ttcnName);
        final IFile actualFile = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
        Module actualModule = projectSourceParser.containedModule(actualFile);
        int insertionOffset = ((TTCN3Module) actualModule).getAssignmentsScope().getLocation().getOffset() + 1;
        MultiTextEdit multiEdit = new MultiTextEdit(insertionOffset, 0);
        RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor(((TTCN3Editor) targetEditor).getDocument(), multiEdit, TextEdit.UPDATE_REGIONS | TextEdit.CREATE_UNDO);
        multiEdit.addChild(new InsertEdit(insertionOffset, "\nimport from " + ttcnName + " all;\n"));
        try {
            processor.performEdits();
        } catch (BadLocationException e) {
            ErrorReporter.logExceptionStackTrace(e);
        }
    } else {
        if (reportDebugInformation) {
            for (DeclarationCollectionHelper foundDeclaration : collected) {
                TITANDebugConsole.println("declaration:" + foundDeclaration.location.getFile() + ": " + foundDeclaration.location.getOffset() + " - " + foundDeclaration.location.getEndOffset() + " is available");
            }
        }
    }
    Display.getDefault().asyncExec(new Runnable() {

        @Override
        public void run() {
            MessageDialog.openWarning(null, "Study feature", "Adding a missing importation is still under study");
        }
    });
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) InsertEdit(org.eclipse.text.edits.InsertEdit) IFile(org.eclipse.core.resources.IFile) DeclarationCollector(org.eclipse.titan.designer.editors.actions.DeclarationCollector) TTCN3ReferenceParser(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3ReferenceParser) TextSelection(org.eclipse.jface.text.TextSelection) Assignments(org.eclipse.titan.designer.AST.Assignments) OpenDeclarationLabelProvider(org.eclipse.titan.designer.editors.actions.OpenDeclarationLabelProvider) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) RewriteSessionEditProcessor(org.eclipse.jface.text.RewriteSessionEditProcessor) ElementListSelectionDialog(org.eclipse.ui.dialogs.ElementListSelectionDialog) Module(org.eclipse.titan.designer.AST.Module) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 68 with Module

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

the class ImportModule method addDeclaration.

/**
 * Adds the imported module or definitions contained in it, to the list
 * declaration proposals.
 *
 * @param declarationCollector
 *                the declaration collector.
 * @param targetModuleId
 *                the identifier of the module where the declaration
 *                should be inserted into.
 */
@Override
public /**
 * {@inheritDoc}
 */
void addDeclaration(final DeclarationCollector declarationCollector, final Identifier targetModuleId) {
    final Module savedReferredModule = referredModule;
    if (savedReferredModule != null) {
        final Assignments assignments = savedReferredModule.getAssignments();
        for (int i = 0; i < assignments.getNofAssignments(); i++) {
            final Assignment temporalAssignment = assignments.getAssignmentByIndex(i);
            if (savedReferredModule.isVisible(CompilationTimeStamp.getBaseTimestamp(), targetModuleId, temporalAssignment)) {
                temporalAssignment.addDeclaration(declarationCollector, 0);
            }
        }
        final Identifier moduleId = declarationCollector.getReference().getModuleIdentifier();
        final List<ISubReference> subrefs = declarationCollector.getReference().getSubreferences();
        if (moduleId == null && subrefs.size() == 1 && identifier.getName().equals(subrefs.get(0).getId().getName())) {
            declarationCollector.addDeclaration(savedReferredModule.getIdentifier().getDisplayName(), savedReferredModule.getIdentifier().getLocation(), (Scope) null);
        }
    }
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) Assignments(org.eclipse.titan.designer.AST.Assignments) Module(org.eclipse.titan.designer.AST.Module)

Example 69 with Module

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

the class ImportModule method checkImports.

@Override
public /**
 * {@inheritDoc}
 */
void checkImports(final CompilationTimeStamp timestamp, final ModuleImportationChain referenceChain, final List<Module> moduleStack) {
    if (null != lastImportCheckTimeStamp && !lastImportCheckTimeStamp.isLess(timestamp)) {
        return;
    }
    symbols.checkUniqueness(timestamp);
    final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(project);
    if (null == parser || null == identifier) {
        lastImportCheckTimeStamp = timestamp;
        // FIXME: is it correct? lastImportCheckTimeStamp will be set in extreme case only - very early running
        referredModule = null;
        return;
    }
    final Module temp = referredModule;
    referredModule = parser.getModuleByName(identifier.getName());
    if (temp != referredModule) {
        setUnhandledChange(true);
    }
    if (referredModule == null) {
        identifier.getLocation().reportSemanticError(MessageFormat.format(MISSINGMODULE, identifier.getDisplayName()));
    } else {
        if (!(referredModule instanceof ASN1Module)) {
            identifier.getLocation().reportSemanticError(MessageFormat.format(NOTASN1MODULE, identifier.getDisplayName()));
            lastImportCheckTimeStamp = timestamp;
            referredModule = null;
            return;
        }
        moduleStack.add(referredModule);
        if (!referenceChain.add(this)) {
            moduleStack.remove(moduleStack.size() - 1);
            lastImportCheckTimeStamp = timestamp;
            return;
        }
        referredModule.checkImports(timestamp, referenceChain, moduleStack);
        for (int i = 0; i < symbols.size(); i++) {
            final Identifier id = symbols.getNthElement(i);
            final List<ISubReference> list = new ArrayList<ISubReference>();
            list.add(new FieldSubReference(id));
            final Defined_Reference reference = new Defined_Reference(null, list);
            reference.setLocation(identifier.getLocation());
            if (null != referredModule.getAssBySRef(timestamp, reference)) {
                if (!((ASN1Module) referredModule).exportsSymbol(timestamp, id)) {
                    identifier.getLocation().reportSemanticError(MessageFormat.format(SYMBOLNOTEXPORTED, id.getDisplayName(), referredModule.getIdentifier().getDisplayName()));
                }
            }
        }
        moduleStack.remove(moduleStack.size() - 1);
    }
    lastImportCheckTimeStamp = timestamp;
}
Also used : ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) Defined_Reference(org.eclipse.titan.designer.AST.ASN1.Defined_Reference) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) ArrayList(java.util.ArrayList) Module(org.eclipse.titan.designer.AST.Module) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Example 70 with Module

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

the class Imports method addDeclaration.

/**
 * Searches for elements that could be referred to be the provided
 * reference, and if found, they are added to the declaration collector.
 * <p>
 *
 * @see DeclarationCollector
 * @param declarationCollector
 *                the declaration collector folding the reference and
 *                collecting the declarations.
 */
public void addDeclaration(final DeclarationCollector declarationCollector) {
    final Reference reference = declarationCollector.getReference();
    final Identifier identifier = reference.getId();
    if (singularImportedSymbols_map.containsKey(identifier.getName())) {
        final Module tempModule = singularImportedSymbols_map.get(identifier.getName());
        tempModule.getAssignmentsScope().addDeclaration(declarationCollector);
    }
    if (!declarationCollector.getCollected().isEmpty()) {
        return;
    }
    for (ImportModule importation : importedModules_map.values()) {
        importation.addDeclaration(declarationCollector, module.getIdentifier());
    }
}
Also used : Identifier(org.eclipse.titan.designer.AST.Identifier) Reference(org.eclipse.titan.designer.AST.Reference) Module(org.eclipse.titan.designer.AST.Module)

Aggregations

Module (org.eclipse.titan.designer.AST.Module)130 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)51 ArrayList (java.util.ArrayList)37 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)36 IFile (org.eclipse.core.resources.IFile)32 Assignment (org.eclipse.titan.designer.AST.Assignment)22 Identifier (org.eclipse.titan.designer.AST.Identifier)21 Location (org.eclipse.titan.designer.AST.Location)16 Reference (org.eclipse.titan.designer.AST.Reference)16 ImportModule (org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule)16 HashMap (java.util.HashMap)14 List (java.util.List)13 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)11 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)11 IProject (org.eclipse.core.resources.IProject)10 IResource (org.eclipse.core.resources.IResource)10 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)10 Assignments (org.eclipse.titan.designer.AST.Assignments)9 Scope (org.eclipse.titan.designer.AST.Scope)9 TextSelection (org.eclipse.jface.text.TextSelection)8