Search in sources :

Example 56 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project KaiZen-OpenAPI-Editor by RepreZen.

the class AbstractJsonHyperlinkDetector method getHyperlinkInfo.

protected HyperlinkInfo getHyperlinkInfo(ITextViewer viewer, IRegion region) {
    final JsonDocument document = (JsonDocument) viewer.getDocument();
    IRegion line;
    try {
        line = document.getLineInformationOfOffset(region.getOffset());
    } catch (BadLocationException e) {
        return null;
    }
    String lineContent;
    try {
        lineContent = document.get(line.getOffset(), line.getLength());
    } catch (BadLocationException e) {
        return null;
    }
    if (lineContent == null || emptyToNull(lineContent) == null) {
        return null;
    }
    final int column = region.getOffset() - line.getOffset();
    final IRegion selected = getSelectedRegion(line, lineContent, column);
    String text;
    try {
        text = document.get(selected.getOffset(), selected.getLength());
    } catch (BadLocationException e) {
        return null;
    }
    if (emptyToNull(text) == null || text.trim().equals(":") || text.trim().equals("$ref:")) {
        return null;
    }
    return new HyperlinkInfo(selected, text, column);
}
Also used : JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 57 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project KaiZen-OpenAPI-Editor by RepreZen.

the class StyledCompletionProposal method apply.

@Override
public void apply(IDocument document) {
    int length = 0;
    int offset = replacementOffset;
    String text = replacementString;
    if (Strings.emptyToNull(prefix) != null) {
        if (replacementString.toLowerCase().startsWith(prefix)) {
            text = replacementString.substring(prefix.length());
        } else if (replacementString.toLowerCase().contains(prefix)) {
            offset = replacementOffset - prefix.length();
            length = prefix.length();
        }
    }
    try {
        document.replace(offset, length, text);
    } catch (BadLocationException x) {
    // ignore
    }
}
Also used : StyledString(org.eclipse.jface.viewers.StyledString) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 58 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project KaiZen-OpenAPI-Editor by RepreZen.

the class SwaggerTemplateContext method createTemplateTranslator.

protected TemplateTranslator createTemplateTranslator() {
    try {
        int offset = getStart();
        IRegion lineRegion = getDocument().getLineInformationOfOffset(offset);
        String line = getDocument().get(lineRegion.getOffset(), lineRegion.getLength());
        int i = 0;
        // support for array items
        StringBuilder indentation = new StringBuilder();
        while (i < line.length()) {
            char indentSymbol = line.charAt(i);
            if (Character.isWhitespace(indentSymbol)) {
                indentation.append(indentSymbol);
                i++;
            } else if ('-' == indentSymbol) {
                // array item
                indentation.append(' ');
                i++;
            } else {
                break;
            }
        }
        if (i != 0)
            return new IndentationAwareTemplateTranslator(indentation.toString());
        return new TemplateTranslator();
    } catch (BadLocationException ex) {
        return new TemplateTranslator();
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 59 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project cubrid-manager by CUBRID.

the class SQLContentAssistProcessor method computeCompletionProposals.

public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    IDocument document = viewer.getDocument();
    int currOffset = offset - 1;
    try {
        String currWord = "";
        char currChar;
        while (currOffset >= 0 && !isKeywordSeparator(currChar = document.getChar(currOffset))) {
            currWord = currChar + currWord;
            currOffset--;
        }
        currWord = trimQualifier(currWord);
        if (currWord.trim().length() == 0 && isSQLEndPos(document, currOffset)) {
            return null;
        }
        // if currWord is "" or AssistProcessor is displaying return null;
        if (currWord.trim().equals("") && isRunning()) {
            return null;
        }
        setRunning(true);
        List<ICompletionProposal> finalProposals = new ArrayList<ICompletionProposal>();
        if (GeneralPreference.isAutoCompleteTablesOrColumns()) {
            // columns
            List<ColumnProposalDetailInfo> tableColumns = getColumnNames(currWord.toUpperCase(Locale.getDefault()), document, offset - 1);
            List<ICompletionProposal> columnProposals = buildColumnProposals(tableColumns, currWord, offset - currWord.length(), CommonUIPlugin.getImage("icons/navigator/table_column_item.png"), false);
            finalProposals.addAll(columnProposals);
            // tables
            List<String> tableNames = getTableNames(currWord.toUpperCase(Locale.getDefault()), document, offset - 1);
            List<ICompletionProposal> tableProposals = buildProposals(tableNames, currWord, offset - currWord.length(), CommonUIPlugin.getImage("icons/navigator/schema_table_item.png"), false);
            finalProposals.addAll(tableProposals);
        }
        // keywords
        if (GeneralPreference.isAutoCompleteKeyword()) {
            List<String> suggestions = wordTracker.suggest(currWord.toUpperCase(Locale.getDefault()), databaseProvider.getDatabaseInfo());
            List<ICompletionProposal> keywordProposals = buildProposals(suggestions, currWord, offset - currWord.length(), CommonUIPlugin.getImage("icons/navigator/sql.png"), true);
            finalProposals.addAll(keywordProposals);
        }
        return finalProposals.toArray(new ICompletionProposal[finalProposals.size()]);
    } catch (BadLocationException e) {
        LOGGER.error("", e);
        lastError = e.getMessage();
        return null;
    } finally {
        setRunning(false);
    }
}
Also used : ICompletionProposal(com.cubrid.common.ui.query.control.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 60 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project AutoRefactor by JnRouvignac.

the class TestHelper method normalizeJavaSourceCode.

public static String normalizeJavaSourceCode(String source) {
    final CodeFormatter codeFormatter = createCodeFormatter(getJava7Options());
    final TextEdit edit = codeFormatter.format(K_COMPILATION_UNIT, // source to format
    source, // source to format
    0, // source to format
    source.length(), // initial indentation and line separator
    0, // initial indentation and line separator
    System.getProperty("line.separator"));
    try {
        final IDocument document = new Document(source);
        edit.apply(document);
        return document.get();
    } catch (MalformedTreeException e) {
        throw new RuntimeException(e);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}
Also used : CodeFormatter(org.eclipse.jdt.core.formatter.CodeFormatter) TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)455 IDocument (org.eclipse.jface.text.IDocument)196 IRegion (org.eclipse.jface.text.IRegion)161 Test (org.junit.Test)102 Position (org.eclipse.jface.text.Position)101 Region (org.eclipse.jface.text.Region)68 Point (org.eclipse.swt.graphics.Point)61 Document (org.eclipse.jface.text.Document)47 CoreException (org.eclipse.core.runtime.CoreException)34 ArrayList (java.util.ArrayList)27 ITypedRegion (org.eclipse.jface.text.ITypedRegion)27 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)22 DocumentEvent (org.eclipse.jface.text.DocumentEvent)21 ITextSelection (org.eclipse.jface.text.ITextSelection)21 StyledText (org.eclipse.swt.custom.StyledText)18 StyledString (org.eclipse.jface.viewers.StyledString)17 IStatus (org.eclipse.core.runtime.IStatus)16 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)15 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)15 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)15