Search in sources :

Example 1 with DeclarationCollectionHelper

use of org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper in project titan.EclipsePlug-ins by eclipse.

the class BaseTextHover method getHoverInfo.

@Override
public String getHoverInfo(final ITextViewer textViewer, final IRegion hoverRegion) {
    if (hoverRegion == null || textViewer == null) {
        return null;
    }
    IAnnotationModel annotationModel = getSourceViewer().getAnnotationModel();
    if (annotationModel != null) {
        Iterator<?> iterator = annotationModel.getAnnotationIterator();
        List<String> messages = new ArrayList<String>();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            if (o instanceof MarkerAnnotation) {
                MarkerAnnotation actualMarker = (MarkerAnnotation) o;
                Position markerPosition = annotationModel.getPosition(actualMarker);
                if (markerPosition != null && markerPosition.getOffset() <= hoverRegion.getOffset() && markerPosition.getOffset() + markerPosition.getLength() >= hoverRegion.getOffset()) {
                    String message = actualMarker.getText();
                    if (message != null) {
                        // Marker error text hover (or tooltip in other words) handles error message
                        // in HTML format, and there can be situation, when the message contains
                        // < and > characters, which are handled as HTML control tags, so they
                        // are not visible. So these < and > characters are removed.
                        // Example: ANTLR sends the following error message during parsing:
                        // "mismatched input 'control' expecting <EOF>"
                        message = message.replaceAll("\\<([A-Z]+)\\>", "$1");
                    } else {
                        ErrorReporter.INTERNAL_ERROR("BaseTextHover.getHoverInfo(): message == null");
                    }
                    messages.add(message);
                }
            }
        }
        if (!messages.isEmpty()) {
            final StringBuilder builder = new StringBuilder();
            builder.append(messages.get(0));
            for (int i = 1; i < messages.size() && i <= 3; i++) {
                builder.append("<br></br>");
                builder.append(messages.get(i));
            }
            if (messages.size() > 3) {
                builder.append("<br></br>...");
            }
            return builder.toString();
        }
    }
    ErrorReporter.parallelDisplayInStatusLine(getTargetEditor(), null);
    DeclarationCollector declarationCollector = OpenDeclarationHelper.findVisibleDeclarations(getTargetEditor(), getReferenceParser(), textViewer.getDocument(), hoverRegion.getOffset(), false);
    if (declarationCollector == null) {
        return null;
    }
    List<DeclarationCollectionHelper> collected = declarationCollector.getCollected();
    // To handle reference problem in T3Doc
    if (T3Doc.isT3DocEnable()) {
        String string = T3Doc.getCommentStringBasedOnReference(declarationCollector, collected, getTargetEditor(), hoverRegion, getReferenceParser(), textViewer);
        if (string != null) {
            return string;
        }
    }
    if (collected.isEmpty()) {
        return null;
    }
    DeclarationCollectionHelper declaration = collected.get(0);
    // Check whether the T3Doc is enabled in the preferences window
    if (!T3Doc.isT3DocEnable()) {
        return declaration.description;
    }
    if (declaration.node != null) {
        if (declaration.node.getT3Doc(declaration.location) == null) {
            return "";
        }
        if (declaration.description != null) {
            return declaration.description + "<BR></BR>" + declaration.node.getT3Doc(declaration.location).toString();
        }
        return declaration.node.getT3Doc(declaration.location).toString();
    }
    if (declaration.scope != null) {
        if (declaration.description != null) {
            return declaration.description + declaration.scope.getT3Doc(declaration.location).toString();
        }
        return declaration.scope.getT3Doc(declaration.location).toString();
    }
    // return declaration.description;
    return "";
}
Also used : DeclarationCollector(org.eclipse.titan.designer.editors.actions.DeclarationCollector) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation)

Example 2 with DeclarationCollectionHelper

use of org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper 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 3 with DeclarationCollectionHelper

use of org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper in project titan.EclipsePlug-ins by eclipse.

the class ImportSelectionDialog method findReferenceInProject.

/**
 * Try to find the declaration of the reference in any module of the
 * project.
 * <p>
 * If exactly one declaration is found, then its location is returned. If
 * multiple modules contain a valid declaration of the reference, then a
 * dialog is displayed to the user to choose one. If none found, or the user
 * cancels the dialog, <code>null</code> is returned.
 * </p>
 *
 * @param reference
 *            The (missing) reference we are searching for.
 * @param project
 *            The project in which we search the declaration.
 * @return The location of the declaration, if uniquely found,
 *         <code>null</code> otherwise.
 */
public static Location findReferenceInProject(final Reference reference, final IProject project) {
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(project);
    final List<DeclarationCollectionHelper> collected = new ArrayList<DeclarationCollectionHelper>();
    final Identifier identifier = reference.getId();
    for (final String moduleName : projectSourceParser.getKnownModuleNames()) {
        final Module m = projectSourceParser.getModuleByName(moduleName);
        if (m != null && m.getAssignments().hasLocalAssignmentWithID(CompilationTimeStamp.getBaseTimestamp(), identifier)) {
            Assignment assignment = m.getAssignments().getLocalAssignmentByID(CompilationTimeStamp.getBaseTimestamp(), identifier);
            if (assignment != null) {
                collected.add(new DeclarationCollectionHelper(assignment.getProposalDescription(), assignment.getIdentifier().getLocation(), assignment));
            }
        }
    }
    Location loc = null;
    if (collected.size() > 1) {
        final List<String> files = new ArrayList<String>();
        for (final DeclarationCollectionHelper c : collected) {
            files.add(c.location.getFile().getName());
        }
        TITANDebugConsole.println("Multiple possible imports for " + reference.getDisplayName() + ": " + files.toString());
        final ImportSelectionDialog dialog = new ImportSelectionDialog(reference, collected, reference.getLocation().getFile());
        Display.getDefault().syncExec(dialog);
        loc = dialog.getSelected();
    } else if (collected.size() == 1) {
        DeclarationCollectionHelper declaration = collected.get(0);
        loc = declaration.location;
        TITANDebugConsole.println("Exactly one module for " + reference.getDisplayName() + " is found: " + loc.getFile().getName());
    } else {
        TITANDebugConsole.println("No imports for " + reference.getDisplayName() + " is found");
    }
    return loc;
}
Also used : DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) Assignment(org.eclipse.titan.designer.AST.Assignment) Identifier(org.eclipse.titan.designer.AST.Identifier) ArrayList(java.util.ArrayList) 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) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) Location(org.eclipse.titan.designer.AST.Location)

Example 4 with DeclarationCollectionHelper

use of org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper in project titan.EclipsePlug-ins by eclipse.

the class T3Doc method getCommentStringBasedOnReference.

public static String getCommentStringBasedOnReference(final DeclarationCollector declarationCollector, final List<DeclarationCollectionHelper> collected, final IEditorPart targetEditor, final IRegion hoverRegion, final IReferenceParser referenceParser, final ITextViewer textViewer) {
    if (!T3Doc.isT3DocEnable()) {
        return null;
    }
    Reference ref = declarationCollector.getReference();
    if (ref == null) {
        return null;
    }
    if ((ref.getMyScope() instanceof NamedBridgeScope || ref.getMyScope() instanceof FormalParameterList) && !collected.isEmpty()) {
        DeclarationCollectionHelper declaration = collected.get(0);
        if (declaration.node instanceof TTCN3_Sequence_Type || declaration.node instanceof FormalParameter) {
            final IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
            ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
            final Module tempModule = projectSourceParser.containedModule(file);
            Assignment ass = tempModule.getEnclosingAssignment(hoverRegion.getOffset());
            if (ass != null) {
                Reference reference = referenceParser.findReferenceForOpening(file, hoverRegion.getOffset(), textViewer.getDocument());
                String str = reference.getDisplayName();
                List<String> al = T3Doc.getCommentStrings(ass.getCommentLocation(), str);
                if (!al.isEmpty()) {
                    final StringBuilder sb = new StringBuilder();
                    for (String string : al) {
                        sb.append(string);
                    }
                    return sb.toString();
                }
            }
        }
    }
    return null;
}
Also used : FormalParameter(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameter) IFile(org.eclipse.core.resources.IFile) Reference(org.eclipse.titan.designer.AST.Reference) TTCN3_Sequence_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Sequence_Type) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) Assignment(org.eclipse.titan.designer.AST.Assignment) FormalParameterList(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameterList) NamedBridgeScope(org.eclipse.titan.designer.AST.NamedBridgeScope) Module(org.eclipse.titan.designer.AST.Module) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)

Example 5 with DeclarationCollectionHelper

use of org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper in project titan.EclipsePlug-ins by eclipse.

the class ImportSelectionDialog method findReferenceInProject.

/**
 * Try to find the declaration of the reference in any module of the
 * project.
 * <p>
 * If exactly one declaration is found, then its location is returned. If
 * multiple modules contain a valid declaration of the reference, then a
 * dialog is displayed to the user to choose one. If none found, or the user
 * cancels the dialog, <code>null</code> is returned.
 * </p>
 *
 * @param reference
 *            The (missing) reference we are searching for.
 * @param project
 *            The project in which we search the declaration.
 * @return The location of the declaration, if uniquely found,
 *         <code>null</code> otherwise.
 */
public static Location findReferenceInProject(final Reference reference, final IProject project) {
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(project);
    final List<DeclarationCollectionHelper> collected = new ArrayList<DeclarationCollectionHelper>();
    final Identifier identifier = reference.getId();
    for (final String moduleName : projectSourceParser.getKnownModuleNames()) {
        final Module m = projectSourceParser.getModuleByName(moduleName);
        if (m != null && m.getAssignments().hasLocalAssignmentWithID(CompilationTimeStamp.getBaseTimestamp(), identifier)) {
            Assignment assignment = m.getAssignments().getLocalAssignmentByID(CompilationTimeStamp.getBaseTimestamp(), identifier);
            if (assignment != null) {
                collected.add(new DeclarationCollectionHelper(assignment.getProposalDescription(), assignment.getIdentifier().getLocation(), assignment));
            }
        }
    }
    Location loc = null;
    if (collected.size() > 1) {
        final List<String> files = new ArrayList<String>();
        for (final DeclarationCollectionHelper c : collected) {
            files.add(c.location.getFile().getName());
        }
        TITANDebugConsole.println("Multiple possible imports for " + reference.getDisplayName() + ": " + files.toString());
        final ImportSelectionDialog dialog = new ImportSelectionDialog(reference, collected, reference.getLocation().getFile());
        Display.getDefault().syncExec(dialog);
        loc = dialog.getSelected();
    } else if (collected.size() == 1) {
        DeclarationCollectionHelper declaration = collected.get(0);
        loc = declaration.location;
        TITANDebugConsole.println("Exactly one module for " + reference.getDisplayName() + " is found: " + loc.getFile().getName());
    } else {
        TITANDebugConsole.println("No imports for " + reference.getDisplayName() + " is found");
    }
    return loc;
}
Also used : DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) Assignment(org.eclipse.titan.designer.AST.Assignment) Identifier(org.eclipse.titan.designer.AST.Identifier) ArrayList(java.util.ArrayList) 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) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

DeclarationCollectionHelper (org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper)5 Module (org.eclipse.titan.designer.AST.Module)4 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)4 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)4 ArrayList (java.util.ArrayList)3 Assignment (org.eclipse.titan.designer.AST.Assignment)3 IFile (org.eclipse.core.resources.IFile)2 Identifier (org.eclipse.titan.designer.AST.Identifier)2 Location (org.eclipse.titan.designer.AST.Location)2 ImportModule (org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule)2 DeclarationCollector (org.eclipse.titan.designer.editors.actions.DeclarationCollector)2 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Position (org.eclipse.jface.text.Position)1 RewriteSessionEditProcessor (org.eclipse.jface.text.RewriteSessionEditProcessor)1 TextSelection (org.eclipse.jface.text.TextSelection)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 InsertEdit (org.eclipse.text.edits.InsertEdit)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 Assignments (org.eclipse.titan.designer.AST.Assignments)1