Search in sources :

Example 6 with Assignments

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

the class OpenDeclaration method handleModuleParameters.

/**
 * Selects and reveals the selected module parameter.
 *
 * @param file
 *                The current file.
 * @param offset
 *                The position of the cursor.
 * @param document
 *                The document opened in the configuration file editor.
 * @return True
 */
public boolean handleModuleParameters(final IFile file, final int offset, final IDocument document) {
    ConfigReferenceParser refParser = new ConfigReferenceParser(false);
    Reference reference = refParser.findReferenceForOpening(file, offset, document);
    if (refParser.isModuleParameter()) {
        if (reference == null) {
            return false;
        }
        ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
        String exactModuleName = refParser.getExactModuleName();
        ArrayList<Assignment> foundAssignments = new ArrayList<Assignment>();
        if (exactModuleName != null) {
            Module module = projectSourceParser.getModuleByName(exactModuleName);
            if (module != null) {
                Assignments assignments = module.getAssignments();
                for (int i = 0; i < assignments.getNofAssignments(); i++) {
                    Assignment assignment = assignments.getAssignmentByIndex(i);
                    if (assignment.getIdentifier().getDisplayName().equals(reference.getId().getDisplayName())) {
                        foundAssignments.add(assignment);
                    }
                }
            }
        } else {
            for (String moduleName : projectSourceParser.getKnownModuleNames()) {
                Module module = projectSourceParser.getModuleByName(moduleName);
                if (module != null) {
                    Assignments assignments = module.getAssignments();
                    for (int i = 0; i < assignments.getNofAssignments(); i++) {
                        Assignment assignment = assignments.getAssignmentByIndex(i);
                        if (assignment.getIdentifier().getDisplayName().equals(reference.getId().getDisplayName())) {
                            foundAssignments.add(assignment);
                        }
                    }
                }
            }
        }
        if (foundAssignments.isEmpty()) {
            targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(NOTMODULEPARDECLARATION);
            return false;
        }
        // List<DeclarationCollectionHelper> collected = declarationCollector.getCollected();
        // DeclarationCollectionHelper declaration = null;
        Assignment assignment = null;
        if (foundAssignments.size() == 1) {
            assignment = foundAssignments.get(0);
        } else {
            Assignment result = openCollectionListDialog(foundAssignments);
            if (result != null) {
                assignment = result;
            }
        }
        IPreferencesService prefs = Platform.getPreferencesService();
        if (prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null)) {
            for (Assignment tempAssignment : foundAssignments) {
                Location location = tempAssignment.getLocation();
                TITANDebugConsole.println("Module parameter: " + location.getFile() + ":" + location.getOffset() + "-" + location.getEndOffset());
            }
        }
        if (assignment != null) {
            Location location = assignment.getLocation();
            selectAndRevealRegion((IFile) location.getFile(), location.getOffset(), location.getEndOffset(), true);
        }
        return true;
    }
    return false;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ConfigReferenceParser(org.eclipse.titan.designer.editors.configeditor.ConfigReferenceParser) Reference(org.eclipse.titan.designer.AST.Reference) ArrayList(java.util.ArrayList) Assignments(org.eclipse.titan.designer.AST.Assignments) Module(org.eclipse.titan.designer.AST.Module) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) CfgLocation(org.eclipse.titan.common.parsers.cfg.CfgLocation) Location(org.eclipse.titan.designer.AST.Location)

Example 7 with Assignments

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

the class ExtractToFunctionWizardFuncNamePage method checkNewNameValidity.

private boolean checkNewNameValidity() {
    final String newName = newFuncName.getText();
    if (newName.length() == 0) {
        setErrorMessage(null);
        setPageComplete(false);
        return false;
    }
    final Module mod = ((ExtractToFunctionRefactoring) getRefactoring()).getSelectedModule();
    switch(mod.getModuletype()) {
        case TTCN3_MODULE:
            if (!Identifier.isValidInTtcn(newName)) {
                setErrorMessage("Not a valid TTCN-3 identifier!");
                setPageComplete(false);
                return false;
            }
            break;
        case ASN_MODULE:
            if (!Identifier.isValidInAsn(newName)) {
                setErrorMessage("Not a valid ASN.1 identifier!");
                setPageComplete(false);
                return false;
            }
            break;
        default:
            ErrorReporter.INTERNAL_ERROR();
    }
    final Assignments assignments = mod.getAssignments();
    for (int i = 0; i < assignments.getNofAssignments(); i++) {
        final Assignment asg = assignments.getAssignmentByIndex(i);
        if (asg.getIdentifier().getDisplayName().equals(newName)) {
            setErrorMessage("A function with the provided name already exists!");
            setPageComplete(false);
            return false;
        }
    }
    setErrorMessage(null);
    setPageComplete(true);
    return true;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) Assignments(org.eclipse.titan.designer.AST.Assignments) Module(org.eclipse.titan.designer.AST.Module)

Example 8 with Assignments

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

the class Embedded_PDV_Type method getTypeRefd.

@Override
public /**
 * {@inheritDoc}
 */
IType getTypeRefd(final CompilationTimeStamp timestamp, final IReferenceChain refChain) {
    if (null == myScope) {
        setIsErroneous(true);
        return this;
    }
    final Identifier identifier = new Identifier(Identifier_type.ID_ASN, EMBEDDED_PDV);
    final Assignments assignments = myScope.getAssignmentsScope();
    if (!assignments.hasAssignmentWithId(timestamp, identifier)) {
        setIsErroneous(true);
        return this;
    }
    final Assignment assignment = assignments.getLocalAssignmentByID(timestamp, identifier);
    if (null == assignment || null == assignment.getType(timestamp)) {
        setIsErroneous(true);
        return this;
    }
    return assignment.getType(timestamp);
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) Identifier(org.eclipse.titan.designer.AST.Identifier) Assignments(org.eclipse.titan.designer.AST.Assignments)

Example 9 with Assignments

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

the class External_Type method getTypeRefd.

@Override
public /**
 * {@inheritDoc}
 */
IType getTypeRefd(final CompilationTimeStamp timestamp, final IReferenceChain refChain) {
    if (null == myScope) {
        setIsErroneous(true);
        return this;
    }
    final Identifier identifier = new Identifier(Identifier_type.ID_ASN, "EXTERNAL");
    final Assignments assignments = myScope.getAssignmentsScope();
    if (!assignments.hasAssignmentWithId(timestamp, identifier)) {
        setIsErroneous(true);
        return this;
    }
    final Assignment assignment = assignments.getLocalAssignmentByID(timestamp, identifier);
    if (null == assignment || null == assignment.getType(timestamp)) {
        setIsErroneous(true);
        return this;
    }
    return assignment.getType(timestamp);
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) Identifier(org.eclipse.titan.designer.AST.Identifier) Assignments(org.eclipse.titan.designer.AST.Assignments)

Example 10 with Assignments

use of org.eclipse.titan.designer.AST.Assignments 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)

Aggregations

Assignments (org.eclipse.titan.designer.AST.Assignments)17 Assignment (org.eclipse.titan.designer.AST.Assignment)13 Module (org.eclipse.titan.designer.AST.Module)9 Identifier (org.eclipse.titan.designer.AST.Identifier)7 ArrayList (java.util.ArrayList)4 ISubReference (org.eclipse.titan.designer.AST.ISubReference)4 ASN1Assignment (org.eclipse.titan.designer.AST.ASN1.ASN1Assignment)3 Undefined_Assignment (org.eclipse.titan.designer.AST.ASN1.Undefined_Assignment)3 Location (org.eclipse.titan.designer.AST.Location)3 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)3 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)2 NULL_Location (org.eclipse.titan.designer.AST.NULL_Location)2 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)2 IFile (org.eclipse.core.resources.IFile)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 RewriteSessionEditProcessor (org.eclipse.jface.text.RewriteSessionEditProcessor)1 TextSelection (org.eclipse.jface.text.TextSelection)1 InsertEdit (org.eclipse.text.edits.InsertEdit)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 CfgLocation (org.eclipse.titan.common.parsers.cfg.CfgLocation)1