Search in sources :

Example 91 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class SmartSemicolonAutoEditStrategy method customizeDocumentCommand.

/*
	 * @see org.eclipse.jface.text.IAutoEditStrategy#customizeDocumentCommand(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.DocumentCommand)
	 */
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
    if (command.text == null)
        return;
    if (command.text.equals(SEMICOLON))
        fCharacter = SEMICHAR;
    else if (command.text.equals(BRACE))
        fCharacter = BRACECHAR;
    else
        return;
    //		IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
    //		if (fCharacter == SEMICHAR && !store.getBoolean(PreferenceConstants.EDITOR_SMART_SEMICOLON))
    //			return;
    //		if (fCharacter == BRACECHAR && !store.getBoolean(PreferenceConstants.EDITOR_SMART_OPENING_BRACE))
    //			return;
    //
    //		IWorkbenchPage page= JavaPlugin.getActivePage();
    //		if (page == null)
    //			return;
    //		IEditorPart part= page.getActiveEditor();
    //		if (!(part instanceof CompilationUnitEditor))
    //			return;
    //		CompilationUnitEditor editor= (CompilationUnitEditor)part;
    //		if (editor.getInsertMode() != ITextEditorExtension3.SMART_INSERT || !editor.isEditable())
    //			return;
    //		ITextEditorExtension2 extension= (ITextEditorExtension2)editor.getAdapter(ITextEditorExtension2.class);
    //		if (extension != null && !extension.validateEditorInputState())
    //			return;
    //		if (isMultilineSelection(document, command))
    //			return;
    // 1: find concerned line / position in java code, location in statement
    int pos = command.offset;
    ITextSelection line;
    try {
        IRegion l = document.getLineInformationOfOffset(pos);
        line = new TextSelection(document, l.getOffset(), l.getLength());
    } catch (BadLocationException e) {
        return;
    }
    // 2: choose action based on findings (is for-Statement?)
    // for now: compute the best position to insert the new character
    int positionInLine = computeCharacterPosition(document, line, pos - line.getOffset(), fCharacter, fPartitioning);
    int position = positionInLine + line.getOffset();
    // never position before the current position!
    if (position < pos)
        return;
    // never double already existing content
    if (alreadyPresent(document, fCharacter, position))
        return;
    // don't do special processing if what we do is actually the normal behaviour
    String insertion = adjustSpacing(document, position, fCharacter);
    if (command.offset == position && insertion.equals(command.text))
        return;
    try {
        //			final SmartBackspaceManager manager= (SmartBackspaceManager) editor.getAdapter(SmartBackspaceManager.class);
        //			if (manager != null && JavaPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_BACKSPACE)) {
        //				TextEdit e1= new ReplaceEdit(command.offset, command.text.length(), document.get(command.offset, command.length));
        //				UndoSpec s1= new UndoSpec(command.offset + command.text.length(),
        //						new Region(command.offset, 0),
        //						new TextEdit[] {e1},
        //						0,
        //						null);
        //
        //				DeleteEdit smart= new DeleteEdit(position, insertion.length());
        //				ReplaceEdit raw= new ReplaceEdit(command.offset, command.length, command.text);
        //				UndoSpec s2= new UndoSpec(position + insertion.length(),
        //						new Region(command.offset + command.text.length(), 0),
        //						new TextEdit[] {smart, raw},
        //						2,
        //						s1);
        //				manager.register(s2);
        //			}
        // 3: modify command
        command.offset = position;
        command.length = 0;
        command.caretOffset = position;
        command.text = insertion;
        command.doit = true;
        command.owner = null;
    } catch (MalformedTreeException e) {
        JavaPlugin.log(e);
    }
}
Also used : ITextSelection(org.eclipse.che.jface.text.ITextSelection) TextSelection(org.eclipse.che.jface.text.TextSelection) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) ITextSelection(org.eclipse.che.jface.text.ITextSelection) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 92 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class TemplateEngine method areMultipleLinesSelected.

/**
	 * Returns <code>true</code> if one line is completely selected or if multiple lines are selected.
	 * Being completely selected means that all characters except the new line characters are
	 * selected.
	 *
	 * @param viewer the text viewer
	 * @return <code>true</code> if one or multiple lines are selected
	 * @since 2.1
	 */
private boolean areMultipleLinesSelected(ITextViewer viewer) {
    if (viewer == null)
        return false;
    Point s = viewer.getSelectedRange();
    if (s.y == 0)
        return false;
    try {
        IDocument document = viewer.getDocument();
        int startLine = document.getLineOfOffset(s.x);
        int endLine = document.getLineOfOffset(s.x + s.y);
        IRegion line = document.getLineInformation(startLine);
        return startLine != endLine || (s.x == line.getOffset() && s.y == line.getLength());
    } catch (BadLocationException x) {
        return false;
    }
}
Also used : Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 93 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class TemplateProposal method validate.

/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
	 */
public boolean validate(IDocument document, int offset, DocumentEvent event) {
    try {
        int replaceOffset = getReplaceOffset();
        if (offset >= replaceOffset) {
            String content = document.get(replaceOffset, offset - replaceOffset);
            String templateName = fTemplate.getName().toLowerCase();
            boolean valid = templateName.startsWith(content.toLowerCase());
            if (!valid && fContext instanceof JavaDocContext && templateName.startsWith("<")) {
                //$NON-NLS-1$
                valid = templateName.startsWith(content.toLowerCase(), 1);
            }
            return valid;
        }
    } catch (BadLocationException e) {
    // concurrent modification - ignore
    }
    return false;
}
Also used : JavaDocContext(org.eclipse.jdt.internal.corext.template.java.JavaDocContext) StyledString(org.eclipse.jface.viewers.StyledString) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 94 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class TemplateProposal method computeRelevance.

/**
     * Computes the relevance to match the relevance values generated by the
     * core content assistant.
     *
     * @return a sensible relevance value.
     */
private int computeRelevance() {
    // see org.eclipse.jdt.internal.codeassist.RelevanceConstants
    final int R_DEFAULT = 0;
    final int R_INTERESTING = 5;
    final int R_CASE = 10;
    final int R_NON_RESTRICTED = 3;
    final int R_EXACT_NAME = 4;
    final int R_INLINE_TAG = 31;
    int base = R_DEFAULT + R_INTERESTING + R_NON_RESTRICTED;
    try {
        if (fContext instanceof DocumentTemplateContext) {
            DocumentTemplateContext templateContext = (DocumentTemplateContext) fContext;
            IDocument document = templateContext.getDocument();
            String content = document.get(fRegion.getOffset(), fRegion.getLength());
            if (content.length() > 0 && fTemplate.getName().startsWith(content))
                base += R_CASE;
            if (fTemplate.getName().equalsIgnoreCase(content))
                base += R_EXACT_NAME;
            if (fContext instanceof JavaDocContext)
                base += R_INLINE_TAG;
        }
    } catch (BadLocationException e) {
    // ignore - not a case sensitive match then
    }
    // see CompletionProposalCollector.computeRelevance
    // just under keywords, but better than packages
    final int TEMPLATE_RELEVANCE = 1;
    return base * 16 + TEMPLATE_RELEVANCE;
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) JavaDocContext(org.eclipse.jdt.internal.corext.template.java.JavaDocContext) StyledString(org.eclipse.jface.viewers.StyledString) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 95 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class TemplateProposal method getAdditionalProposalInfo.

/*
	 * @see ICompletionProposal#getAdditionalProposalInfo()
	 */
public String getAdditionalProposalInfo() {
    try {
        fContext.setReadOnly(true);
        TemplateBuffer templateBuffer;
        try {
            templateBuffer = fContext.evaluate(fTemplate);
        } catch (TemplateException e) {
            return null;
        }
        IDocument document = new Document(templateBuffer.getString());
        IndentUtil.indentLines(document, new LineRange(0, document.getNumberOfLines()), null, null);
        StringBuffer buffer = new StringBuffer();
        HTMLPrinter.insertPageProlog(buffer, 0, JavadocFinder.getStyleSheet());
        HTMLPrinter.addParagraph(buffer, document.get());
        HTMLPrinter.addPageEpilog(buffer);
        return buffer.toString();
    } catch (BadLocationException e) {
        //			handleException(
        //					JavaPlugin.getActiveWorkbenchShell(), new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "", e))); //$NON-NLS-1$
        JavaPlugin.log(e);
        return null;
    }
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) LineRange(org.eclipse.che.jface.text.source.LineRange) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)133 IDocument (org.eclipse.jface.text.IDocument)58 IRegion (org.eclipse.jface.text.IRegion)43 Document (org.eclipse.jface.text.Document)26 Point (org.eclipse.swt.graphics.Point)17 CoreException (org.eclipse.core.runtime.CoreException)16 Position (org.eclipse.jface.text.Position)13 StyledString (org.eclipse.jface.viewers.StyledString)13 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)13 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)12 TemplateException (org.eclipse.jface.text.templates.TemplateException)12 TextEdit (org.eclipse.text.edits.TextEdit)12 UndoEdit (org.eclipse.text.edits.UndoEdit)10 Region (org.eclipse.jface.text.Region)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)8 ArrayList (java.util.ArrayList)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)6 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)6 DefaultLineTracker (org.eclipse.jface.text.DefaultLineTracker)6