Search in sources :

Example 71 with Location

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

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

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

Example 74 with Location

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

the class StatementNode method isLocationEqualTo.

public boolean isLocationEqualTo(final StatementNode sn) {
    if (!(this.getAstNode() instanceof ILocateableNode)) {
        return false;
    }
    if (!(sn.getAstNode() instanceof ILocateableNode)) {
        return false;
    }
    final Location loc0 = ((ILocateableNode) this.getAstNode()).getLocation();
    final Location loc1 = ((ILocateableNode) sn.getAstNode()).getLocation();
    return loc0.getFile().equals(loc1.getFile()) && loc0.getOffset() == loc1.getOffset() && loc0.getEndOffset() == loc1.getEndOffset();
}
Also used : ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) Location(org.eclipse.titan.designer.AST.Location)

Example 75 with Location

use of org.eclipse.titan.designer.AST.Location 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<Definition> nodes = vis.getLocations();
    if (nodes.isEmpty()) {
        return null;
    }
    // calculate edit locations
    final List<Definition> locations = new ArrayList<Definition>();
    try {
        final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
        job1.join();
    } catch (InterruptedException ie) {
        ErrorReporter.logExceptionStackTrace(ie);
    } catch (CoreException ce) {
        ErrorReporter.logError("UngroupModuleparRefactoring/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);
    int precedeOffset = -1;
    final String fileContents = loadFileContent(toVisit);
    for (Definition node : locations) {
        final Location l = node.getCumulativeDefinitionLocation();
        final Location typeLocation = node.getType(CompilationTimeStamp.getBaseTimestamp()).getLocation();
        final Location identifierLocation = node.getIdentifier().getLocation();
        if (precedeOffset != l.getOffset()) {
            precedeOffset = l.getOffset();
            final int len = l.getEndOffset() - l.getOffset();
            rootEdit.addChild(new DeleteEdit(l.getOffset(), len + 1));
        }
        String typeText = fileContents.substring(typeLocation.getOffset(), typeLocation.getEndOffset()).trim();
        String name = fileContents.substring(identifierLocation.getOffset(), identifierLocation.getEndOffset()).trim();
        String newModulePar = "";
        if (node instanceof Def_ModulePar) {
            Def_ModulePar modulePar = (Def_ModulePar) node;
            if (modulePar.getDefaultValue() != null) {
                final Location valueLocation = modulePar.getDefaultValue().getLocation();
                String valueText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
                newModulePar = "modulepar " + typeText + " " + name + " := " + valueText + ";\n";
            } else {
                newModulePar = "modulepar " + typeText + " " + name + ";\n";
            }
        } else if (node instanceof Def_ModulePar_Template) {
            Def_ModulePar_Template modulePar = (Def_ModulePar_Template) node;
            if (modulePar.getDefaultTemplate() != null) {
                final Location valueLocation = modulePar.getDefaultTemplate().getLocation();
                String temlateText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
                newModulePar = "modulepar template " + typeText + " " + name + " := " + temlateText + ";\n";
            } else {
                newModulePar = "modulepar template " + typeText + " " + name + ";\n";
            }
        }
        rootEdit.addChild(new InsertEdit(l.getOffset(), newModulePar));
    }
    return tfc;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) Def_ModulePar_Template(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar_Template) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) ArrayList(java.util.ArrayList) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Def_ModulePar(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) CoreException(org.eclipse.core.runtime.CoreException) Module(org.eclipse.titan.designer.AST.Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

Location (org.eclipse.titan.designer.AST.Location)109 Identifier (org.eclipse.titan.designer.AST.Identifier)24 ReParseException (org.eclipse.titan.designer.parsers.ttcn3parser.ReParseException)24 NULL_Location (org.eclipse.titan.designer.AST.NULL_Location)21 IIdentifierReparser (org.eclipse.titan.designer.parsers.ttcn3parser.IIdentifierReparser)18 IdentifierReparser (org.eclipse.titan.designer.parsers.ttcn3parser.IdentifierReparser)18 Module (org.eclipse.titan.designer.AST.Module)16 ArrayList (java.util.ArrayList)15 IFile (org.eclipse.core.resources.IFile)13 HashMap (java.util.HashMap)11 Reference (org.eclipse.titan.designer.AST.Reference)10 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)10 Assignment (org.eclipse.titan.designer.AST.Assignment)9 IDocument (org.eclipse.jface.text.IDocument)8 InsertEdit (org.eclipse.text.edits.InsertEdit)8 ILocateableNode (org.eclipse.titan.designer.AST.ILocateableNode)8 CoreException (org.eclipse.core.runtime.CoreException)7 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)7 TITANMarker (org.eclipse.titan.common.parsers.TITANMarker)7 Definition (org.eclipse.titan.designer.AST.TTCN3.definitions.Definition)7