Search in sources :

Example 1 with Pattern

use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.

the class Link method removeHost.

/**
    * If the URL has the same scheme, hostname, and port as the current page,
    * then drop them from the URL.
    *
    * For example,
    *    http://rstudio.com/help/base/ls
    * becomes
    *    /help/base/ls
    */
private String removeHost(String url) {
    String pageUrl = Document.get().getURL();
    Pattern p = Pattern.create("^http(s?)://[^/]+");
    Match m = p.match(pageUrl, 0);
    if (m == null) {
        assert false : "Couldn't parse page URL: " + url;
        return url;
    }
    String prefix = m.getValue();
    if (!url.startsWith(prefix))
        return url;
    else
        return url.substring(prefix.length());
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 2 with Pattern

use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.

the class TextEditingTarget method extractIndentation.

private String extractIndentation(String code) {
    Pattern leadingWhitespace = Pattern.create("^(\\s*)");
    Match match = leadingWhitespace.match(code, 0);
    return match == null ? "" : match.getGroup(1);
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 3 with Pattern

use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.

the class TextEditingTarget method fixupCodeBeforeSaving.

private void fixupCodeBeforeSaving() {
    int lineCount = docDisplay_.getRowCount();
    if (lineCount < 1)
        return;
    if (docDisplay_.hasActiveCollabSession()) {
        // these preferences don't apply to shared editing sessions
        return;
    }
    if (prefs_.stripTrailingWhitespace().getValue() && !fileType_.isMarkdown() && !name_.getValue().equals("DESCRIPTION")) {
        String code = docDisplay_.getCode();
        Pattern pattern = Pattern.create("[ \t]+$");
        String strippedCode = pattern.replaceAll(code, "");
        if (!strippedCode.equals(code)) {
            // Calling 'setCode' can remove folds in the document; cache the folds
            // and reapply them after document mutation.
            JsArray<AceFold> folds = docDisplay_.getFolds();
            docDisplay_.setCode(strippedCode, true);
            for (AceFold fold : JsUtil.asIterable(folds)) docDisplay_.addFold(fold.getRange());
        }
    }
    if (prefs_.autoAppendNewline().getValue() || fileType_.isPython()) {
        String lastLine = docDisplay_.getLine(lineCount - 1);
        if (lastLine.length() != 0)
            docDisplay_.insertCode(docDisplay_.getEnd().getEnd(), "\n");
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) AceFold(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold) JsArrayString(com.google.gwt.core.client.JsArrayString) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

Example 4 with Pattern

use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.

the class TextEditingTarget method isRChunk.

private boolean isRChunk(Scope scope) {
    String labelText = docDisplay_.getLine(scope.getPreamble().getRow());
    Pattern reEngine = Pattern.create(".*engine\\s*=\\s*['\"]([^'\"]*)['\"]");
    Match match = reEngine.match(labelText, 0);
    if (match == null)
        return true;
    String engine = match.getGroup(1).toLowerCase();
    // collect those here.
    return engine.equals("r");
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) JsArrayString(com.google.gwt.core.client.JsArrayString) Match(org.rstudio.core.client.regex.Match)

Example 5 with Pattern

use of org.rstudio.core.client.regex.Pattern in project rstudio by rstudio.

the class TextEditingTarget method reflowComments.

private void reflowComments(String commentPrefix, final boolean multiParagraphIndent, InputEditorSelection selection, final InputEditorPosition cursorPos) {
    String code = docDisplay_.getCode(selection);
    String[] lines = code.split("\n");
    String prefix = StringUtil.getCommonPrefix(lines, true, false);
    Pattern pattern = Pattern.create("^\\s*" + commentPrefix + "+('?)\\s*");
    Match match = pattern.match(prefix, 0);
    // Selection includes non-comments? Abort.
    if (match == null)
        return;
    prefix = match.getValue();
    final boolean roxygen = match.hasGroup(1);
    int cursorRowIndex = 0;
    int cursorColIndex = 0;
    if (cursorPos != null) {
        cursorRowIndex = selectionToPosition(cursorPos).getRow() - selectionToPosition(selection.getStart()).getRow();
        cursorColIndex = Math.max(0, cursorPos.getPosition() - prefix.length());
    }
    final WordWrapCursorTracker wwct = new WordWrapCursorTracker(cursorRowIndex, cursorColIndex);
    int maxLineLength = prefs_.printMarginColumn().getValue() - prefix.length();
    WordWrap wordWrap = new WordWrap(maxLineLength, false) {

        @Override
        protected boolean forceWrapBefore(String line) {
            String trimmed = line.trim();
            if (roxygen && trimmed.startsWith("@") && !trimmed.startsWith("@@")) {
                // Roxygen tags always need to be at the start of a line. If
                // there is content immediately following the roxygen tag, then
                // content should be wrapped until the next roxygen tag is
                // encountered.
                indent_ = "";
                if (TAG_WITH_CONTENTS.match(line, 0) != null) {
                    indentRestOfLines_ = true;
                }
                return true;
            } else // empty line disables indentation
            if (!multiParagraphIndent && (line.trim().length() == 0)) {
                indent_ = "";
                indentRestOfLines_ = false;
            }
            return super.forceWrapBefore(line);
        }

        @Override
        protected void onChunkWritten(String chunk, int insertionRow, int insertionCol, int indexInOriginalString) {
            if (indentRestOfLines_) {
                indentRestOfLines_ = false;
                // TODO: Use real indent from settings
                indent_ = "  ";
            }
            wwct.onChunkWritten(chunk, insertionRow, insertionCol, indexInOriginalString);
        }

        private boolean indentRestOfLines_ = false;

        private Pattern TAG_WITH_CONTENTS = Pattern.create("@\\w+\\s+[^\\s]");
    };
    for (String line : lines) {
        String content = line.substring(Math.min(line.length(), prefix.length()));
        if (content.matches("^\\s*\\@examples\\b.*$"))
            wordWrap.setWrappingEnabled(false);
        else if (content.trim().startsWith("@"))
            wordWrap.setWrappingEnabled(true);
        wwct.onBeginInputRow();
        wordWrap.appendLine(content);
    }
    String wrappedString = wordWrap.getOutput();
    StringBuilder finalOutput = new StringBuilder();
    for (String line : StringUtil.getLineIterator(wrappedString)) finalOutput.append(prefix).append(line).append("\n");
    // Remove final \n
    if (finalOutput.length() > 0)
        finalOutput.deleteCharAt(finalOutput.length() - 1);
    String reflowed = finalOutput.toString();
    docDisplay_.setSelection(selection);
    if (!reflowed.equals(code)) {
        docDisplay_.replaceSelection(reflowed);
    }
    if (cursorPos != null) {
        if (wwct.getResult() != null) {
            int row = wwct.getResult().getY();
            int col = wwct.getResult().getX();
            row += selectionToPosition(selection.getStart()).getRow();
            col += prefix.length();
            Position pos = Position.create(row, col);
            docDisplay_.setSelection(docDisplay_.createSelection(pos, pos));
        } else {
            docDisplay_.collapseSelection(false);
        }
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) JsArrayString(com.google.gwt.core.client.JsArrayString) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Match(org.rstudio.core.client.regex.Match)

Aggregations

Pattern (org.rstudio.core.client.regex.Pattern)28 Match (org.rstudio.core.client.regex.Match)22 JsArrayString (com.google.gwt.core.client.JsArrayString)8 ArrayList (java.util.ArrayList)3 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)3 Point (org.rstudio.core.client.Point)2 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)2 Iterator (java.util.Iterator)1 TextCursor (org.rstudio.core.client.TextCursor)1 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)1 EditableFileType (org.rstudio.studio.client.common.filetypes.EditableFileType)1 FileType (org.rstudio.studio.client.common.filetypes.FileType)1 TextFileType (org.rstudio.studio.client.common.filetypes.TextFileType)1 OpenFileInBrowserEvent (org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserEvent)1 ServerError (org.rstudio.studio.client.server.ServerError)1 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)1 AceFold (org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold)1 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)1