Search in sources :

Example 86 with Module

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

the class ImportSelectionDialog method createFileChange.

private Change createFileChange(final IFile toVisit) {
    if (toVisit == null) {
        return null;
    }
    final String designerId = ProductConstants.PRODUCT_ID_DESIGNER;
    final String displayDebugInfo = org.eclipse.titan.designer.preferences.PreferenceConstants.DISPLAYDEBUGINFORMATION;
    reportDebug = Platform.getPreferencesService().getBoolean(designerId, displayDebugInfo, false, null);
    final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
    final Module module = sourceParser.containedModule(toVisit);
    if (module == null || !(module instanceof TTCN3Module)) {
        return null;
    }
    final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
    IDocument doc;
    final TTCN3Module tModule = (TTCN3Module) module;
    try {
        doc = tfc.getCurrentDocument(null);
        final MultiTextEdit resultEdit = organizeImportsEdit(tModule, doc);
        if (!resultEdit.hasChildren()) {
            return null;
        }
        tfc.setEdit(resultEdit);
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace("Error while organizing imports", e);
    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return tfc;
}
Also used : TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) CoreException(org.eclipse.core.runtime.CoreException) 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) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 87 with Module

use of org.eclipse.titan.designer.AST.Module 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 88 with Module

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

the class SelectionFinder method findSelection.

private Definition findSelection() {
    // getting the active editor
    final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (editor == null || !(editor instanceof TTCN3Editor)) {
        return null;
    }
    final TTCN3Editor targetEditor = (TTCN3Editor) editor;
    // iterating through part of the module
    final IResource selectedRes = extractResource(targetEditor);
    if (!(selectedRes instanceof IFile)) {
        ErrorReporter.logError("SelectionFinder.findSelection(): Selected resource `" + selectedRes.getName() + "' is not a file.");
        return null;
    }
    final IFile selectedFile = (IFile) selectedRes;
    sourceProj = selectedFile.getProject();
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(sourceProj);
    final Module selectedModule = projectSourceParser.containedModule(selectedFile);
    // getting current selection
    final ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
    final TextSelection textSelection = extractSelection(selectionService.getSelection());
    // getting current selection nodes
    final int selectionOffset = textSelection.getOffset() + textSelection.getLength();
    final SelectionFinderVisitor selVisitor = new SelectionFinderVisitor(selectionOffset);
    selectedModule.accept(selVisitor);
    final Definition selectedDef = selVisitor.getSelection();
    if (selectedDef == null) {
        ErrorReporter.logWarning("SelectionFinder.findSelection(): Visitor did not find a definition in the selection.");
        final IStatusLineManager statusLineManager = targetEditor.getEditorSite().getActionBars().getStatusLineManager();
        statusLineManager.setErrorMessage(ERR_MSG_NO_SELECTION);
        return null;
    }
    return selectedDef;
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) IFile(org.eclipse.core.resources.IFile) IStatusLineManager(org.eclipse.jface.action.IStatusLineManager) TextSelection(org.eclipse.jface.text.TextSelection) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) ISelectionService(org.eclipse.ui.ISelectionService) IEditorPart(org.eclipse.ui.IEditorPart) Module(org.eclipse.titan.designer.AST.Module) IResource(org.eclipse.core.resources.IResource) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Example 89 with Module

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

the class ChangeCreator method perform.

public void perform() {
    // get module in selected file
    if (file == null) {
        return;
    }
    final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(file.getProject());
    final Module module = sourceParser.containedModule(file);
    if (module == null) {
        return;
    }
    // 
    if (textSelection != null) {
        performOnSelectionOnly(module);
    } else {
        performOnWholeModule(module);
    }
}
Also used : Module(org.eclipse.titan.designer.AST.Module) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Example 90 with Module

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

the class ChangeCreator method createFileChange.

private Change createFileChange(final IFile toVisit) {
    if (toVisit == null) {
        return null;
    }
    final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
    final Module module = sourceParser.containedModule(toVisit);
    if (module == null) {
        return null;
    }
    // find all locations in the module that should be edited
    final DefinitionVisitor vis = new DefinitionVisitor();
    module.accept(vis);
    final NavigableSet<ILocateableNode> nodes = vis.getLocations();
    if (nodes.isEmpty()) {
        return null;
    }
    // calculate edit locations
    final List<Location> locations = new ArrayList<Location>();
    try {
        final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
        job1.join();
    } catch (InterruptedException ie) {
        ErrorReporter.logExceptionStackTrace(ie);
    } catch (CoreException ce) {
        ErrorReporter.logError("MinimizeVisibilityRefactoring/CreateChange.createFileChange(): " + "CoreException while calculating edit locations. ");
        ErrorReporter.logExceptionStackTrace(ce);
    }
    if (locations.isEmpty()) {
        return null;
    }
    // create a change for each edit location
    final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
    final MultiTextEdit rootEdit = new MultiTextEdit();
    tfc.setEdit(rootEdit);
    for (Location l : locations) {
        final int len = l.getEndOffset() - l.getOffset();
        if (len == 0) {
            rootEdit.addChild(new InsertEdit(l.getOffset(), "private "));
        } else {
            rootEdit.addChild(new ReplaceEdit(l.getOffset(), len, "private "));
        }
    }
    return tfc;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) ArrayList(java.util.ArrayList) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) CoreException(org.eclipse.core.runtime.CoreException) ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) Module(org.eclipse.titan.designer.AST.Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Location(org.eclipse.titan.designer.AST.Location)

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