use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class RoxygenHelper method insertRoxygenSkeletonS4Class.
private void insertRoxygenSkeletonS4Class(TokenCursor cursor) {
final Position startPos = cursor.currentPosition();
String setClassCall = extractCall(cursor);
if (setClassCall == null)
return;
server_.getSetClassCall(setClassCall, new ServerRequestCallback<SetClassCall>() {
@Override
public void onResponseReceived(SetClassCall response) {
if (hasRoxygenBlock(startPos)) {
amendExistingRoxygenBlock(startPos.getRow() - 1, response.getClassName(), response.getSlots(), null, "slot", RE_ROXYGEN_SLOT);
} else {
insertRoxygenTemplate(response.getClassName(), response.getSlots(), response.getTypes(), "slot", "S4 class", startPos);
}
}
@Override
public void onError(ServerError error) {
Debug.logError(error);
}
});
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class MathJaxUtil method getLatexRange.
public static Range getLatexRange(DocDisplay docDisplay, Position pos) {
if (pos == null)
pos = docDisplay.getCursorPosition();
// find start of latex block
TokenIterator startIt = docDisplay.createTokenIterator();
// avoid case where token iterator moves back across lines
// to discover a latex block
Token startToken = startIt.moveToPosition(pos);
if (startToken != null && startToken.hasAllTypes("latex", "end") && startIt.getCurrentTokenRow() != pos.getRow()) {
return null;
}
for (Token token = startIt.moveToPosition(pos); token != null; token = startIt.stepBackward()) {
if (!token.hasType("latex"))
return null;
if (token.hasType("begin"))
break;
}
// find end of latex block
TokenIterator endIt = docDisplay.createTokenIterator();
for (Token token = endIt.moveToPosition(pos); token != null; token = endIt.stepForward()) {
if (!token.hasType("latex"))
return null;
if (token.hasType("end"))
break;
}
Token lhsToken = startIt.getCurrentToken();
if (lhsToken == null || !lhsToken.hasAllTypes("latex", "begin"))
return null;
Token rhsToken = endIt.getCurrentToken();
if (rhsToken == null || !rhsToken.hasAllTypes("latex", "end"))
return null;
Position startPos = startIt.getCurrentTokenPosition();
Position endPos = endIt.getCurrentTokenPosition();
endPos.setColumn(endPos.getColumn() + endIt.getCurrentToken().getValue().length());
return Range.fromPoints(startPos, endPos);
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class YamlFrontMatter method getFrontMatterRange.
public static Range getFrontMatterRange(DocDisplay display) {
// front matter can end with ... rather than ---; see spec:
// http://www.yaml.org/spec/1.2/spec.html#id2760395
RegExp frontMatterBegin = RegExp.compile("^---\\s*$", "gm");
RegExp frontMatterEnd = RegExp.compile("^(---|\\.\\.\\.)\\s*$", "gm");
Position begin = null;
Position end = null;
for (int i = 0; i < display.getRowCount(); i++) {
String code = display.getLine(i);
if (begin == null) {
// haven't found front matter begin yet; test this line
MatchResult beginMatch = frontMatterBegin.exec(code);
if (beginMatch == null) {
// Markdown package)
if (!code.matches("\\s*"))
break;
} else {
begin = Position.create(i + 1, 0);
continue;
}
} else if (end == null) {
// haven't found front matter end yet; test this line
MatchResult endMatch = frontMatterEnd.exec(code);
if (endMatch != null) {
end = Position.create(i, 0);
break;
}
}
}
if (begin == null || end == null)
return null;
return Range.fromPoints(begin, end);
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class RCompletionManager method checkCanAutoPopup.
private boolean checkCanAutoPopup(char c, int lookbackLimit) {
if (docDisplay_.isVimModeOn() && !docDisplay_.isVimInInsertMode())
return false;
String currentLine = docDisplay_.getCurrentLine();
Position cursorPos = input_.getCursorPosition();
int cursorColumn = cursorPos.getColumn();
// Don't auto-popup when the cursor is within a string
if (docDisplay_.isCursorInSingleLineString())
return false;
// be annoying)
if (isValidForRIdentifier(docDisplay_.getCharacterAtCursor()))
return false;
boolean canAutoPopup = (currentLine.length() > lookbackLimit - 1 && isValidForRIdentifier(c));
if (isConsole_ && !uiPrefs_.alwaysCompleteInConsole().getValue())
canAutoPopup = false;
if (canAutoPopup) {
for (int i = 0; i < lookbackLimit; i++) {
if (!isValidForRIdentifier(currentLine.charAt(cursorColumn - i - 1))) {
canAutoPopup = false;
break;
}
}
}
return canAutoPopup;
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class RCompletionManager method getCurrentCompletionToken.
String getCurrentCompletionToken() {
AceEditor editor = (AceEditor) docDisplay_;
if (editor == null)
return "";
// `r foo`
if (DocumentMode.isCursorInMarkdownMode(docDisplay_))
return token_;
Position cursorPos = editor.getCursorPosition();
Token currentToken = editor.getSession().getTokenAt(cursorPos);
if (currentToken == null)
return "";
// If the user has inserted some spaces, the cursor might now lie
// on a 'text' token. In that case, find the previous token and
// use that for completion.
String suffix = "";
if (currentToken.getValue().trim().isEmpty()) {
suffix = currentToken.getValue();
TokenIterator it = editor.createTokenIterator();
it.moveToPosition(cursorPos);
Token token = it.stepBackward();
if (token != null)
currentToken = token;
}
// Exclude non-string and non-identifier tokens.
if (currentToken.hasType("operator", "comment", "numeric", "text", "punctuation"))
return "";
String tokenValue = currentToken.getValue();
String subsetted = tokenValue.substring(0, cursorPos.getColumn() - currentToken.getColumn());
return subsetted + suffix;
}
Aggregations