Search in sources :

Example 26 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange 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 27 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.

the class ExtractModuleParRefactoring method createChange.

private WorkspaceJob createChange() {
    final WorkspaceJob job = new WorkspaceJob("ExtractModulePar: writing to target project") {

        @Override
        public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
            if (copyMap == null) {
                ErrorReporter.logError("ExtractModuleParRefactoring::createChange(): Reading dependencies did not finish.");
                return Status.CANCEL_STATUS;
            }
            if (filesToCopy == null) {
                ErrorReporter.logError("ExtractModuleParRefactoring::createChange(): Reading dependencies did not finish (2).");
                return Status.CANCEL_STATUS;
            }
            // create files & write dependencies in them
            for (Entry<IPath, StringBuilder> e : copyMap.entrySet()) {
                IFile targetFile = createFile(e.getKey());
                if (targetFile == null) {
                    ErrorReporter.logError("Unable to create file `" + e.getKey() + "' while extracting module parameters.");
                    return Status.CANCEL_STATUS;
                }
                TextFileChange change = new TextFileChange("extract_append", targetFile);
                MultiTextEdit rootEdit = new MultiTextEdit();
                change.setEdit(rootEdit);
                TextEdit edit = new InsertEdit(0, e.getValue().toString());
                rootEdit.addChild(edit);
                change.perform(new NullProgressMonitor());
            }
            // copy files from 'filesToCopy' without opening them
            for (IFile f : filesToCopy) {
                if (copyFile(f) == null) {
                    ErrorReporter.logError("Unable to copy file `" + (f == null ? "null" : f.getProjectRelativePath()) + "' while extracting module aprameters.");
                    return Status.CANCEL_STATUS;
                }
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.schedule();
    return job;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) InsertEdit(org.eclipse.text.edits.InsertEdit) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 28 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.

the class ChangeCreator method performOnSelectionOnly.

private void performOnSelectionOnly(final Module module) {
    final Location selLoc = new Location(file, textSelection.getStartLine(), textSelection.getOffset(), textSelection.getOffset() + textSelection.getLength());
    final SelectionVisitor vis = new SelectionVisitor(selLoc);
    module.accept(vis);
    final Map<Log_Statement, Context> res = vis.getResult();
    final MultiTextEdit rootEdit = new MultiTextEdit();
    for (Map.Entry<Log_Statement, Context> e : res.entrySet()) {
        final TextEdit edit = createTextEdit(e.getKey(), e.getValue());
        if (edit != null) {
            rootEdit.addChild(edit);
        }
    }
    if (rootEdit.hasChildren()) {
        change = new TextFileChange("Context logging", file);
        change.setEdit(rootEdit);
    }
}
Also used : Context(org.eclipse.titanium.refactoring.logging.context.Context) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) Map(java.util.Map) Log_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Log_Statement) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Location(org.eclipse.titan.designer.AST.Location)

Example 29 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.

the class ChangeCreator method performOnWholeModule.

private void performOnWholeModule(final Module module) {
    final ContextFinder vis = new ContextFinder(settings);
    module.accept(vis);
    final Map<Log_Statement, Context> res = vis.getResult();
    final MultiTextEdit rootEdit = new MultiTextEdit();
    for (Map.Entry<Log_Statement, Context> e : res.entrySet()) {
        final TextEdit edit = createTextEdit(e.getKey(), e.getValue());
        if (edit != null) {
            rootEdit.addChild(edit);
        }
    }
    if (rootEdit.hasChildren()) {
        change = new TextFileChange("Context logging", file);
        change.setEdit(rootEdit);
    }
}
Also used : Context(org.eclipse.titanium.refactoring.logging.context.Context) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) Map(java.util.Map) Log_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Log_Statement) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 30 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange 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

TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)37 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)18 TextChange (org.eclipse.ltk.core.refactoring.TextChange)12 IFile (org.eclipse.core.resources.IFile)11 CoreException (org.eclipse.core.runtime.CoreException)11 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)10 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)10 TextEdit (org.eclipse.text.edits.TextEdit)9 InsertEdit (org.eclipse.text.edits.InsertEdit)8 Module (org.eclipse.titan.designer.AST.Module)8 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)6 ArrayList (java.util.ArrayList)5 IDocument (org.eclipse.jface.text.IDocument)5 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)3 IProject (org.eclipse.core.resources.IProject)3