use of org.rstudio.core.client.Pair in project rstudio by rstudio.
the class FindResult method getLineHTML.
public final SafeHtml getLineHTML() {
SafeHtmlBuilder out = new SafeHtmlBuilder();
ArrayList<Integer> on = getMatchOns();
ArrayList<Integer> off = getMatchOffs();
ArrayList<Pair<Boolean, Integer>> parts = new ArrayList<Pair<Boolean, Integer>>();
while (on.size() + off.size() > 0) {
int onVal = on.size() == 0 ? Integer.MAX_VALUE : on.get(0);
int offVal = off.size() == 0 ? Integer.MAX_VALUE : off.get(0);
if (onVal <= offVal)
parts.add(new Pair<Boolean, Integer>(true, on.remove(0)));
else
parts.add(new Pair<Boolean, Integer>(false, off.remove(0)));
}
String line = getLineValue();
// Use a counter to ensure tags are balanced.
int openTags = 0;
for (int i = 0; i < line.length(); i++) {
while (parts.size() > 0 && parts.get(0).second == i) {
if (parts.remove(0).first) {
out.appendHtmlConstant("<strong>");
openTags++;
} else if (openTags > 0) {
out.appendHtmlConstant("</strong>");
openTags--;
}
}
out.append(line.charAt(i));
}
while (openTags > 0) {
openTags--;
out.appendHtmlConstant("</strong>");
}
return out.toSafeHtml();
}
use of org.rstudio.core.client.Pair in project rstudio by rstudio.
the class TextEditingTargetReformatHelper method getAlignmentRanges.
private ArrayList<Pair<Integer, Integer>> getAlignmentRanges() {
int selectionStart = docDisplay_.getSelectionStart().getRow();
int selectionEnd = docDisplay_.getSelectionEnd().getRow();
ArrayList<Pair<Integer, Integer>> ranges = new ArrayList<Pair<Integer, Integer>>();
for (int i = selectionStart; i <= selectionEnd; i++) {
String line = docDisplay_.getLine(i);
String masked = StringUtil.maskStrings(line);
Match match = DELIM_PATTERN.match(masked, 0);
if (match != null) {
String delimiter = match.getGroup(2);
int rangeStart = i;
while (i++ <= selectionEnd) {
line = docDisplay_.getLine(i);
masked = StringUtil.maskStrings(line);
// Allow empty lines, and comments, to live within the range.
if (masked.matches("^\\s*$") || masked.matches("^\\s*#.*$"))
continue;
// If this line doesn't match, bail
match = DELIM_PATTERN.match(masked, 0);
if (match == null || !match.getGroup(2).equals(delimiter))
break;
}
// But don't allow comments or whitespaces to exist at the
// end of a range.
int rangeEnd = i - 1;
line = docDisplay_.getLine(rangeEnd);
while (line.matches("^\\s*$") || line.matches("^\\s*#.*$")) {
rangeEnd--;
line = docDisplay_.getLine(rangeEnd);
}
ranges.add(new Pair<Integer, Integer>(rangeStart, rangeEnd));
}
}
return ranges;
}
use of org.rstudio.core.client.Pair in project rstudio by rstudio.
the class ShortcutsEmitter method printShortcut.
private void printShortcut(SourceWriter writer, String condition, String shortcut, String command, String shortcutGroup, String title, String disableModes) throws UnableToCompleteException {
List<Pair<Integer, String>> keys = new ArrayList<Pair<Integer, String>>();
for (String keyCombination : shortcut.split("\\s+")) {
String[] chunks = keyCombination.split("\\+");
// Build the shortcut modifiers integer and validate
// as we build.
int modifiers = KeyboardShortcut.NONE;
for (int i = 0; i < chunks.length - 1; i++) {
String m = chunks[i];
if (m.equals("Ctrl"))
modifiers += KeyboardShortcut.CTRL;
else if (m.equals("Alt"))
modifiers += KeyboardShortcut.ALT;
else if (m.equals("Shift"))
modifiers += KeyboardShortcut.SHIFT;
else if (m.equals("Meta"))
modifiers += KeyboardShortcut.META;
else {
logger_.log(Type.ERROR, "Invalid modifier '" + m + "'; expected one of " + "'Ctrl', 'Alt', 'Shift', 'Meta'");
throw new UnableToCompleteException();
}
}
// Validate the key name.
String key = toKey(chunks[chunks.length - 1]);
if (key == null) {
logger_.log(Type.ERROR, "Invalid shortcut '" + shortcut + "', only " + "modified alphanumeric characters, enter, " + "left, right, up, down, pageup, pagedown, " + "and tab are valid");
throw new UnableToCompleteException();
}
// Push the parsed keys to the list.
keys.add(new Pair<Integer, String>(modifiers, key));
}
// Emit the relevant code registering these shortcuts.
if (!condition.isEmpty()) {
writer.println("if (" + condition + ") {");
writer.indent();
}
if (keys.size() == 1) {
int modifiers = keys.get(0).first;
String key = keys.get(0).second;
writer.println("ShortcutManager.INSTANCE.register(" + modifiers + ", " + key + ", " + command + ", " + "\"" + shortcutGroup + "\", " + "\"" + title + "\", " + "\"" + disableModes + "\");");
} else if (keys.size() == 2) {
int m1 = keys.get(0).first;
String k1 = keys.get(0).second;
int m2 = keys.get(1).first;
String k2 = keys.get(1).second;
writer.println("ShortcutManager.INSTANCE.register(" + m1 + ", " + k1 + ", " + m2 + ", " + k2 + ", " + command + ", " + "\"" + shortcutGroup + "\", " + "\"" + title + "\", " + "\"" + disableModes + "\");");
} else {
logger_.log(Type.ERROR, "Invalid key sequence: sequences must be of length 1 or 2");
throw new UnableToCompleteException();
}
if (!condition.isEmpty()) {
writer.outdent();
writer.println("}");
}
}
use of org.rstudio.core.client.Pair in project rstudio by rstudio.
the class AddinsCommandManager method registerBindings.
private void registerBindings(final EditorKeyBindings bindings, final CommandWithArg<EditorKeyBindings> afterLoad) {
List<Pair<List<KeySequence>, CommandBinding>> commands = new ArrayList<Pair<List<KeySequence>, CommandBinding>>();
RAddins rAddins = MainWindowObject.rAddins().get();
for (String id : bindings.iterableKeys()) {
List<KeySequence> keyList = bindings.get(id).getKeyBindings();
RAddin addin = rAddins.get(id);
if (addin == null)
continue;
CommandBinding binding = new AddinCommandBinding(addin);
commands.add(new Pair<List<KeySequence>, CommandBinding>(keyList, binding));
}
KeyMap map = ShortcutManager.INSTANCE.getKeyMap(KeyMapType.ADDIN);
for (int i = 0; i < commands.size(); i++) {
map.setBindings(commands.get(i).first, commands.get(i).second);
}
if (afterLoad != null)
afterLoad.execute(bindings);
}
use of org.rstudio.core.client.Pair in project rstudio by rstudio.
the class TextEditingTargetReformatHelper method alignAssignment.
void alignAssignment() {
InputEditorSelection initialSelection = docDisplay_.getSelection();
ArrayList<Pair<Integer, Integer>> ranges = getAlignmentRanges();
if (ranges.isEmpty())
return;
for (Pair<Integer, Integer> range : ranges) doAlignAssignment(range.first, range.second);
docDisplay_.setSelection(initialSelection.extendToLineStart().extendToLineEnd());
}
Aggregations