Search in sources :

Example 96 with TextSelection

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

the class SelectionFinder method performHeadless.

void performHeadless(final IFile selFile, final ITextSelection textSel) {
    selectedFile = selFile;
    final String fileContents = readFileContents(selFile);
    if (fileContents == null) {
        ErrorReporter.logError("ExtractToFunctionRefactoring.findSelection(): selFile does not exist at: " + selFile.getFullPath());
        return;
    }
    final IDocument doc = new Document(fileContents);
    textSelection = new TextSelection(doc, textSel.getOffset(), textSel.getLength());
    // 
    project = selFile.getProject();
    sourceParser = GlobalParser.getProjectSourceParser(project);
    selectedModule = sourceParser.containedModule(selectedFile);
    if (selectedModule == null) {
        ErrorReporter.logError("ExtractToFunctionRefactoring.findSelection(): The module in the file " + selectedFile.getName() + " has no name.");
        return;
    }
    // iterating through the module for the selected statements
    final SelectionVisitor selectionVisitor = new SelectionVisitor(textSelection.getOffset(), textSelection.getLength());
    selectedModule.accept(selectionVisitor);
    selectedStatements = selectionVisitor.createStatementList(textSelection);
    if (selectedStatements.isEmpty()) {
        ErrorReporter.logError(ERR_MSG_NO_SELECTION);
        return;
    }
    if (ExtractToFunctionRefactoring.DEBUG_MESSAGES_ON) {
        ErrorReporter.logError(selectedStatements.createDebugInfo());
        ErrorReporter.logError(createDebugInfo());
    }
    // finding return type & runs on clause
    final RunsOnClauseFinder runsonVisitor = new RunsOnClauseFinder(selectedStatements.getLocation());
    selectedModule.accept(runsonVisitor);
    runsOnRef = runsonVisitor.getRunsOnRef();
    parentFunc = runsonVisitor.getFuncDef();
    // finding insert location
    if (parentFunc instanceof Definition) {
        insertLoc = ((Definition) parentFunc).getLocation().getEndOffset();
    } else if (parentFunc instanceof ControlPart) {
        final ControlPart cp = (ControlPart) parentFunc;
        final Location commentLoc = cp.getCommentLocation();
        insertLoc = commentLoc == null ? cp.getLocation().getOffset() : commentLoc.getOffset();
    }
    // 
    final ReturnVisitor retVis = new ReturnVisitor();
    selectedStatements.accept(retVis);
    returnCertainty = retVis.getCertainty();
    if (retVis.getCertainty() != ReturnCertainty.NO) {
        returnType = runsonVisitor.getReturnType();
    }
    // checking erroneousness of selection
    checkErroneousGoto();
    if (containsBreakWithoutLoop()) {
        warnings.add(new RefactoringStatusEntry(RefactoringStatus.WARNING, WARNING_ERRONEOUS_BREAK));
    }
    if (containsContinueWithoutLoop()) {
        warnings.add(new RefactoringStatusEntry(RefactoringStatus.WARNING, WARNING_ERRONEOUS_CONTINUE));
    }
    if (retVis.getCertainty() == ReturnCertainty.MAYBE) {
        warnings.add(new RefactoringStatusEntry(RefactoringStatus.WARNING, WARNING_UNCERTAIN_RETURN));
    }
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) ControlPart(org.eclipse.titan.designer.AST.TTCN3.definitions.ControlPart) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) Location(org.eclipse.titan.designer.AST.Location)

Example 97 with TextSelection

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

the class OpenDeclaration method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    targetEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (targetEditor instanceof ConfigEditor) {
        targetEditor = ((ConfigEditor) targetEditor).getEditor();
    }
    if (targetEditor == null || !(targetEditor instanceof ConfigTextEditor)) {
        return null;
    }
    targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(null);
    IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
    if (file == null) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(NOTIDENTIFIABLEFILE);
        return null;
    }
    if (!TITANNature.hasTITANNature(file.getProject())) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(TITANNature.NO_TITAN_FILE_NATURE_FOUND);
        return null;
    }
    int offset;
    if (!selection.isEmpty() && selection instanceof TextSelection && !"".equals(((TextSelection) selection).getText())) {
        IPreferencesService prefs = Platform.getPreferencesService();
        if (prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null)) {
            TITANDebugConsole.println("Selected: " + ((TextSelection) selection).getText());
        }
        TextSelection textSelection = (TextSelection) selection;
        offset = textSelection.getOffset() + textSelection.getLength();
    } else {
        offset = ((ConfigTextEditor) targetEditor).getCarretOffset();
    }
    IDocument document = ((ConfigTextEditor) targetEditor).getDocument();
    section_type section = getSection(document, offset);
    if (section_type.UNKNOWN.equals(section)) {
        if (handleModuleParameters(file, offset, document)) {
            return null;
        }
        return null;
    } else if (section_type.INCLUDE.equals(section)) {
        handleIncludes(file, offset, document);
        return null;
    } else if (section_type.MODULE_PARAMETERS.equals(section)) {
        // identifiable as a module parameter.
        if (handleModuleParameters(file, offset, document)) {
            return null;
        }
    }
    // Fall back.
    handleDefinitions(file, offset, document);
    return null;
}
Also used : CfgInterval.section_type(org.eclipse.titan.common.parsers.cfg.CfgInterval.section_type) IFile(org.eclipse.core.resources.IFile) TextSelection(org.eclipse.jface.text.TextSelection) ConfigEditor(org.eclipse.titan.designer.editors.configeditor.ConfigEditor) ConfigTextEditor(org.eclipse.titan.designer.editors.configeditor.ConfigTextEditor) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) IDocument(org.eclipse.jface.text.IDocument)

Example 98 with TextSelection

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

the class OpenDeclaration method run.

@Override
public void run(final IAction action) {
    if (targetEditor instanceof ConfigEditor) {
        targetEditor = ((ConfigEditor) targetEditor).getEditor();
    }
    if (targetEditor == null || !(targetEditor instanceof ConfigTextEditor)) {
        return;
    }
    targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(null);
    IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
    if (file == null) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(NOTIDENTIFIABLEFILE);
        return;
    }
    if (!TITANNature.hasTITANNature(file.getProject())) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(TITANNature.NO_TITAN_FILE_NATURE_FOUND);
        return;
    }
    int offset;
    if (!selection.isEmpty() && selection instanceof TextSelection && !"".equals(((TextSelection) selection).getText())) {
        IPreferencesService prefs = Platform.getPreferencesService();
        if (prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null)) {
            TITANDebugConsole.println("Selected: " + ((TextSelection) selection).getText());
        }
        TextSelection textSelection = (TextSelection) selection;
        offset = textSelection.getOffset() + textSelection.getLength();
    } else {
        offset = ((ConfigTextEditor) targetEditor).getCarretOffset();
    }
    IDocument document = ((ConfigTextEditor) targetEditor).getDocument();
    section_type section = getSection(document, offset);
    if (section_type.UNKNOWN.equals(section)) {
        return;
    } else if (section_type.INCLUDE.equals(section)) {
        handleIncludes(file, offset, document);
        return;
    } else if (section_type.MODULE_PARAMETERS.equals(section)) {
        // identifiable as a module parameter.
        if (handleModuleParameters(file, offset, document)) {
            return;
        }
    }
    // Fall back.
    handleDefinitions(file, offset, document);
}
Also used : CfgInterval.section_type(org.eclipse.titan.common.parsers.cfg.CfgInterval.section_type) IFile(org.eclipse.core.resources.IFile) TextSelection(org.eclipse.jface.text.TextSelection) ConfigEditor(org.eclipse.titan.designer.editors.configeditor.ConfigEditor) ConfigTextEditor(org.eclipse.titan.designer.editors.configeditor.ConfigTextEditor) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) IDocument(org.eclipse.jface.text.IDocument)

Example 99 with TextSelection

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

the class GotoMatchingBracketAction method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (activeEditor instanceof ASN1Editor) {
        this.targetEditor = (ASN1Editor) activeEditor;
    } else {
        this.targetEditor = null;
    }
    if (activeEditor == null) {
        return null;
    }
    if (!selection.isEmpty()) {
        if (selection instanceof TextSelection) {
            TextSelection tSelection = (TextSelection) selection;
            if (tSelection.getLength() != 0) {
                return null;
            }
        }
    }
    IDocument document = this.targetEditor.getDocument();
    int carretOffset = this.targetEditor.getCarretOffset();
    PairMatcher pairMatcher = new PairMatcher();
    IRegion region = pairMatcher.match(document, carretOffset);
    if (region == null) {
        return null;
    }
    int targetOffset;
    if (region.getOffset() + 1 == carretOffset) {
        targetOffset = region.getOffset() + region.getLength();
    } else {
        targetOffset = region.getOffset() + 1;
    }
    this.targetEditor.setCarretOffset(targetOffset);
    this.targetEditor.selectAndReveal(targetOffset, 0);
    return null;
}
Also used : ASN1Editor(org.eclipse.titan.designer.editors.asn1editor.ASN1Editor) PairMatcher(org.eclipse.titan.designer.editors.asn1editor.PairMatcher) TextSelection(org.eclipse.jface.text.TextSelection) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 100 with TextSelection

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

the class OpenDeclaration method doOpenDeclaration.

private final void doOpenDeclaration() {
    if (targetEditor == null || !(targetEditor instanceof ASN1Editor)) {
        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;
    }
    if (ResourceExclusionHelper.isExcluded(file)) {
        MessageDialog.openError(null, "Open Declaration does not work within excluded resources", "This module is excluded from build. To use the Open Declaration " + "feature please click on the 'Toggle exclude from build state' in the context menu of the Project Explorer. ");
        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 = ((ASN1Editor) targetEditor).getCarretOffset();
    }
    ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
    final Module module = projectSourceParser.containedModule(file);
    if (module == null) {
        // TODO: How could this happen??? (NPE occured)
        return;
    }
    IdentifierFinderVisitor visitor = new IdentifierFinderVisitor(offset);
    module.accept(visitor);
    final Declaration decl = visitor.getReferencedDeclaration();
    if (decl == null) {
        if (reportDebugInformation) {
            TITANDebugConsole.println("No visible elements found");
        }
        return;
    }
    selectAndRevealDeclaration(decl.getIdentifier().getLocation());
}
Also used : ASN1Editor(org.eclipse.titan.designer.editors.asn1editor.ASN1Editor) IFile(org.eclipse.core.resources.IFile) IdentifierFinderVisitor(org.eclipse.titan.designer.declarationsearch.IdentifierFinderVisitor) TextSelection(org.eclipse.jface.text.TextSelection) Declaration(org.eclipse.titan.designer.declarationsearch.Declaration) Module(org.eclipse.titan.designer.AST.Module) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Aggregations

TextSelection (org.eclipse.jface.text.TextSelection)105 IFile (org.eclipse.core.resources.IFile)35 ITextSelection (org.eclipse.jface.text.ITextSelection)24 ISelection (org.eclipse.jface.viewers.ISelection)24 Test (org.junit.Test)23 IDocument (org.eclipse.jface.text.IDocument)21 IEditorPart (org.eclipse.ui.IEditorPart)19 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Path (org.eclipse.core.runtime.Path)13 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)11 BadLocationException (org.eclipse.jface.text.BadLocationException)10 IRegion (org.eclipse.jface.text.IRegion)10 XtextResource (org.eclipse.xtext.resource.XtextResource)9 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)8 Module (org.eclipse.titan.designer.AST.Module)8 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)8 XExpression (org.eclipse.xtext.xbase.XExpression)8 TTCN3Editor (org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor)7 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)7