Search in sources :

Example 6 with ILocateableNode

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

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

the class ChangeCreator method calculateEditLocations.

private WorkspaceJob calculateEditLocations(final NavigableSet<ILocateableNode> nodes, final IFile file, final MultiTextEdit rootEdit) throws CoreException {
    final WorkspaceJob job = new WorkspaceJob("InsertFieldRefactoring: calculate edit locations") {

        @Override
        public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
            for (ILocateableNode node : nodes) {
                int vmLen = settings.getType().length() + settings.getId().getTtcnName().length();
                if (node instanceof Def_Type) {
                    Def_Type df = (Def_Type) node;
                    Type type = df.getType(CompilationTimeStamp.getBaseTimestamp());
                    if (type instanceof TTCN3_Sequence_Type || type instanceof TTCN3_Set_Type) {
                        vmLen = insertField((TTCN3_Set_Seq_Choice_BaseType) type, node, rootEdit, vmLen);
                    }
                } else if (node instanceof Sequence_Value) {
                    Sequence_Value sv = (Sequence_Value) node;
                    vmLen += 6;
                    final Location nodeLocation = node.getLocation();
                    if (settings.getPosition() < sv.getNofComponents()) {
                        final Location valueLocation = sv.getSeqValueByIndex(settings.getPosition()).getLocation();
                        Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), valueLocation.getOffset(), valueLocation.getEndOffset() + vmLen);
                        rootEdit.addChild(new InsertEdit(l.getOffset(), settings.getId().getTtcnName() + " := " + settings.getValue() + ", "));
                    } else {
                        int max = sv.getNofComponents();
                        final Location valueLocation = sv.getSeqValueByIndex(max - 1).getLocation();
                        Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), valueLocation.getEndOffset(), valueLocation.getEndOffset() + vmLen);
                        rootEdit.addChild(new InsertEdit(l.getOffset(), ", " + settings.getId().getTtcnName() + " := " + settings.getValue()));
                    }
                } else if (node instanceof TTCN3Template) {
                    TTCN3Template template = (TTCN3Template) node;
                    vmLen += 6;
                    if (template instanceof Named_Template_List) {
                        Named_Template_List ntl = (Named_Template_List) template;
                        final Location nodeLocation = node.getLocation();
                        if (settings.getPosition() < ntl.getNofTemplates()) {
                            final Location templateLocation = ntl.getTemplateByIndex(settings.getPosition()).getLocation();
                            Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), templateLocation.getOffset(), templateLocation.getEndOffset() + vmLen);
                            rootEdit.addChild(new InsertEdit(l.getOffset(), settings.getId().getTtcnName() + " := " + settings.getValue() + ", "));
                        } else {
                            int max = ntl.getNofTemplates();
                            final Location templateLocation = ntl.getTemplateByIndex(max - 1).getLocation();
                            Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), templateLocation.getEndOffset(), templateLocation.getEndOffset() + vmLen);
                            rootEdit.addChild(new InsertEdit(l.getOffset(), ", " + settings.getId().getTtcnName() + " := " + settings.getValue()));
                        }
                    } else if (template instanceof Template_List) {
                        Template_List tl = (Template_List) template;
                        final Location nodeLocation = node.getLocation();
                        if (settings.getPosition() < tl.getNofTemplates()) {
                            final Location templateLocation = tl.getTemplateByIndex(settings.getPosition()).getLocation();
                            Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), templateLocation.getOffset(), templateLocation.getEndOffset() + vmLen);
                            rootEdit.addChild(new InsertEdit(l.getOffset(), settings.getValue() + ","));
                        } else {
                            int max = tl.getNofTemplates();
                            final Location templateLocation = tl.getTemplateByIndex(max - 1).getLocation();
                            Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), templateLocation.getEndOffset(), templateLocation.getEndOffset() + vmLen);
                            rootEdit.addChild(new InsertEdit(l.getOffset(), "," + settings.getValue()));
                        }
                    }
                }
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.schedule();
    return job;
}
Also used : Def_Type(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Type) InsertEdit(org.eclipse.text.edits.InsertEdit) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Sequence_Value(org.eclipse.titan.designer.AST.TTCN3.values.Sequence_Value) TTCN3_Sequence_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Sequence_Type) TTCN3_Set_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Set_Type) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TTCN3_Set_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Set_Type) TTCN3_Set_Seq_Choice_BaseType(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Set_Seq_Choice_BaseType) TTCN3_Sequence_Type(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Sequence_Type) Type(org.eclipse.titan.designer.AST.Type) Def_Type(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Type) Template_List(org.eclipse.titan.designer.AST.TTCN3.templates.Template_List) Named_Template_List(org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List) ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) TTCN3Template(org.eclipse.titan.designer.AST.TTCN3.templates.TTCN3Template) Named_Template_List(org.eclipse.titan.designer.AST.TTCN3.templates.Named_Template_List) TTCN3_Set_Seq_Choice_BaseType(org.eclipse.titan.designer.AST.TTCN3.types.TTCN3_Set_Seq_Choice_BaseType) Location(org.eclipse.titan.designer.AST.Location)

Example 8 with ILocateableNode

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

the class ChangeCreator method calculateEditLocations.

private WorkspaceJob calculateEditLocations(final NavigableSet<ILocateableNode> nodes, final IFile file, final List<Location> locations_out) throws CoreException {
    final WorkspaceJob job = new WorkspaceJob("MinimizeVisibilityRefactoring: calculate edit locations") {

        @Override
        public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
            final int BUF_LEN = 8;
            try {
                InputStream is = file.getContents();
                InputStreamReader isr = new InputStreamReader(is);
                int currOffset = 0;
                char[] content = new char[BUF_LEN];
                for (ILocateableNode node : nodes) {
                    int toSkip = node.getLocation().getOffset() - currOffset;
                    if (toSkip < 0) {
                        ErrorReporter.logError("MinimizeVisibilityRefactoring.ChangeCreator: Negative skip value" + " while parsing file: " + file.getName() + ", offset: " + node.getLocation().getOffset() + "->" + currOffset);
                        continue;
                    }
                    isr.skip(toSkip);
                    isr.read(content);
                    String str = new String(content);
                    VisibilityModifier vm = parseVisibilityModifier(str);
                    int vmLen = 0;
                    if (vm != null) {
                        switch(vm) {
                            case Public:
                                vmLen = 7;
                                break;
                            case Friend:
                                vmLen = 7;
                                break;
                            case Private:
                                vmLen = 8;
                                break;
                        }
                    }
                    locations_out.add(new Location(node.getLocation().getFile(), node.getLocation().getLine(), node.getLocation().getOffset(), node.getLocation().getOffset() + vmLen));
                    currOffset = node.getLocation().getOffset() + BUF_LEN;
                }
                isr.close();
                is.close();
            } catch (IOException ioe) {
                ErrorReporter.logError("MinimizeVisibilityRefactoring.CreateChange: " + "Error while reading source project.");
                return Status.CANCEL_STATUS;
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.schedule();
    return job;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) VisibilityModifier(org.eclipse.titan.designer.AST.TTCN3.definitions.VisibilityModifier) IOException(java.io.IOException) Location(org.eclipse.titan.designer.AST.Location)

Example 9 with ILocateableNode

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

Example 10 with ILocateableNode

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

the class BlockNode method toString.

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("BN(").append(astNode.toString()).append("), loc: ");
    if (astNode instanceof ILocateableNode) {
        final Location loc = ((ILocateableNode) astNode).getLocation();
        sb.append(loc.getOffset()).append('-').append(loc.getEndOffset()).append(';');
    } else {
        sb.append("<none>;");
    }
    sb.append(" parent: ");
    if (parent == null) {
        sb.append("<null>");
    } else {
        sb.append("SN(").append(parent.astNode.toString()).append(')');
    }
    return sb.toString();
}
Also used : ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

ILocateableNode (org.eclipse.titan.designer.AST.ILocateableNode)11 Location (org.eclipse.titan.designer.AST.Location)8 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)5 InsertEdit (org.eclipse.text.edits.InsertEdit)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)3 Module (org.eclipse.titan.designer.AST.Module)3 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 CoreException (org.eclipse.core.runtime.CoreException)2 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)2 Def_Type (org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Type)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 IFile (org.eclipse.core.resources.IFile)1