Search in sources :

Example 1 with RewriteSessionEditProcessor

use of org.eclipse.jface.text.RewriteSessionEditProcessor in project xtext-eclipse by eclipse.

the class ReconcilingUnitOfWork method exec.

@Override
public T exec(XtextResource state) throws Exception {
    String original = document.get();
    DocumentChangeListener documentChangeListener = new DocumentChangeListener();
    T result;
    try {
        document.addDocumentListener(documentChangeListener);
        // lazy linking URIs might change, so resolve everything before applying any changes
        EcoreUtil2.resolveAll(state, CancelIndicator.NullImpl);
        composer.beginRecording(state);
        result = work.exec(state);
        final TextEdit edit = composer.endRecording();
        if (edit != null) {
            if (documentChangeListener.hasDocumentChanged())
                throw new IllegalStateException("Cannot modify document textually and semantically within the same unit of work");
            RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS | TextEdit.CREATE_UNDO);
            processor.performEdits();
        }
    } catch (RuntimeException e) {
        document.set(original);
        throw e;
    } catch (Exception e) {
        document.set(original);
        throw new RuntimeException(e);
    } finally {
        document.removeDocumentListener(documentChangeListener);
    }
    return result;
}
Also used : RewriteSessionEditProcessor(org.eclipse.jface.text.RewriteSessionEditProcessor) TextEdit(org.eclipse.text.edits.TextEdit)

Example 2 with RewriteSessionEditProcessor

use of org.eclipse.jface.text.RewriteSessionEditProcessor in project titan.EclipsePlug-ins by eclipse.

the class AbstractIndentAction method doIndent.

/**
 * Do the actual indentation work.
 */
private void doIndent() {
    if (targetEditor == null) {
        return;
    }
    final IDocument document = getDocument();
    if (null == document) {
        return;
    }
    Interval rootInterval = GlobalIntervalHandler.getInterval(document);
    if (rootInterval == null) {
        rootInterval = (new HeuristicalIntervalDetector()).buildIntervals(document);
        GlobalIntervalHandler.putInterval(document, rootInterval);
    }
    if (rootInterval == null) {
        return;
    }
    int startLine = -1;
    int endLine = -1;
    if (!selection.isEmpty()) {
        if (selection instanceof TextSelection) {
            final TextSelection tSelection = (TextSelection) selection;
            if (tSelection.getLength() != 0) {
                startLine = tSelection.getStartLine();
                endLine = tSelection.getEndLine();
            }
        }
    }
    if (startLine == -1 || endLine == -1) {
        startLine = 0;
        endLine = document.getNumberOfLines() - 1;
    }
    indentArray.clear();
    indentArray.add("");
    final int nofLines = endLine - startLine;
    int offset;
    int realOffset;
    int lineLength;
    Interval interval;
    try {
        final int regionStartOffset = document.getLineOffset(startLine);
        final int regionLength = document.getLineOffset(endLine) + document.getLineLength(endLine) - regionStartOffset;
        multiEdit = new MultiTextEdit(regionStartOffset, regionLength);
        final RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor(document, multiEdit, TextEdit.UPDATE_REGIONS | TextEdit.CREATE_UNDO);
        for (int i = nofLines; i >= 0; i--) {
            lineLength = document.getLineLength(startLine + i);
            offset = document.getLineOffset(startLine + i);
            realOffset = getRealLineStart(document, offset, lineLength);
            interval = rootInterval.getSmallestEnclosingInterval(realOffset);
            final int indentLevel = lineIndentationLevel(document, realOffset, offset + lineLength, interval);
            setIndentLevel(document, document.getLineOffset(startLine + i), indentLevel);
        }
        performEdits(processor);
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
    return;
}
Also used : RewriteSessionEditProcessor(org.eclipse.jface.text.RewriteSessionEditProcessor) TextSelection(org.eclipse.jface.text.TextSelection) HeuristicalIntervalDetector(org.eclipse.titan.designer.editors.HeuristicalIntervalDetector) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) Interval(org.eclipse.titan.common.parsers.Interval)

Example 3 with RewriteSessionEditProcessor

use of org.eclipse.jface.text.RewriteSessionEditProcessor 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

RewriteSessionEditProcessor (org.eclipse.jface.text.RewriteSessionEditProcessor)3 BadLocationException (org.eclipse.jface.text.BadLocationException)2 TextSelection (org.eclipse.jface.text.TextSelection)2 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)2 IFile (org.eclipse.core.resources.IFile)1 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)1 IDocument (org.eclipse.jface.text.IDocument)1 InsertEdit (org.eclipse.text.edits.InsertEdit)1 TextEdit (org.eclipse.text.edits.TextEdit)1 Interval (org.eclipse.titan.common.parsers.Interval)1 Assignments (org.eclipse.titan.designer.AST.Assignments)1 Module (org.eclipse.titan.designer.AST.Module)1 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)1 HeuristicalIntervalDetector (org.eclipse.titan.designer.editors.HeuristicalIntervalDetector)1 DeclarationCollectionHelper (org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper)1 DeclarationCollector (org.eclipse.titan.designer.editors.actions.DeclarationCollector)1 OpenDeclarationLabelProvider (org.eclipse.titan.designer.editors.actions.OpenDeclarationLabelProvider)1 TTCN3Editor (org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor)1 TTCN3ReferenceParser (org.eclipse.titan.designer.editors.ttcn3editor.TTCN3ReferenceParser)1 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)1