| /*=============================================================================# |
| # Copyright (c) 2009, 2020 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.r.ui.sourceediting; |
| |
| import java.util.List; |
| |
| import org.eclipse.jface.text.BadLocationException; |
| import org.eclipse.jface.text.IDocument; |
| import org.eclipse.jface.text.link.LinkedModeModel; |
| import org.eclipse.jface.text.link.LinkedPosition; |
| |
| import org.eclipse.statet.jcommons.collections.ImCollections; |
| |
| import org.eclipse.statet.ecommons.text.TextUtil; |
| import org.eclipse.statet.ecommons.text.core.sections.DocContentSections; |
| import org.eclipse.statet.ecommons.text.ui.BracketLevel; |
| |
| |
| public final class RBracketLevel extends BracketLevel { |
| |
| |
| public static final class CurlyBracketPosition extends InBracketPosition { |
| |
| public CurlyBracketPosition(final IDocument doc, final int offset, final int length, |
| final int sequence) { |
| super(doc, offset, length, sequence); |
| } |
| |
| @Override |
| public char getOpenChar() { |
| return '{'; |
| } |
| |
| @Override |
| public char getCloseChar() { |
| return '}'; |
| } |
| |
| } |
| |
| public static final class RoundBracketPosition extends InBracketPosition { |
| |
| public RoundBracketPosition(final IDocument doc, final int offset, final int length, |
| final int sequence) { |
| super(doc, offset, length, sequence); |
| } |
| |
| @Override |
| public char getOpenChar() { |
| return '('; |
| } |
| |
| @Override |
| public char getCloseChar() { |
| return ')'; |
| } |
| |
| } |
| |
| public static final class SquareBracketPosition extends InBracketPosition { |
| |
| public SquareBracketPosition(final IDocument doc, final int offset, final int length, |
| final int sequence) { |
| super(doc, offset, length, sequence); |
| } |
| |
| @Override |
| public char getOpenChar() { |
| return '['; |
| } |
| |
| @Override |
| public char getCloseChar() { |
| return ']'; |
| } |
| |
| } |
| |
| |
| public final static class StringDPosition extends InBracketPosition { |
| |
| public StringDPosition(final IDocument doc, final int offset, final int length, |
| final int sequence) { |
| super(doc, offset, length, sequence); |
| } |
| |
| @Override |
| public char getOpenChar() { |
| return '"'; |
| } |
| |
| @Override |
| public char getCloseChar() { |
| return '"'; |
| } |
| |
| @Override |
| public boolean insertCR(final int charOffset) { |
| return false; |
| } |
| |
| @Override |
| protected boolean isEscaped(final int offset) throws BadLocationException { |
| return (TextUtil.countBackward(getDocument(), offset, '\\') % 2 == 1); |
| } |
| |
| } |
| |
| public final static class StringSPosition extends InBracketPosition { |
| |
| public StringSPosition(final IDocument doc, final int offset, final int length, |
| final int sequence) { |
| super(doc, offset, length, sequence); |
| } |
| |
| @Override |
| public char getOpenChar() { |
| return '\''; |
| } |
| |
| @Override |
| public char getCloseChar() { |
| return '\''; |
| } |
| |
| @Override |
| public boolean insertCR(final int charOffset) { |
| return false; |
| } |
| |
| @Override |
| protected boolean isEscaped(final int offset) throws BadLocationException { |
| return (TextUtil.countBackward(getDocument(), offset, '\\') % 2 == 1); |
| } |
| |
| } |
| |
| public final static class QuotedPosition extends InBracketPosition { |
| |
| public QuotedPosition(final IDocument doc, final int offset, final int length, |
| final int sequence) { |
| super(doc, offset, length, sequence); |
| } |
| |
| @Override |
| public char getOpenChar() { |
| return '`'; |
| } |
| |
| @Override |
| public char getCloseChar() { |
| return '`'; |
| } |
| |
| @Override |
| public boolean insertCR(final int charOffset) { |
| return false; |
| } |
| |
| @Override |
| protected boolean isEscaped(final int offset) throws BadLocationException { |
| return (TextUtil.countBackward(getDocument(), offset, '\\') % 2 == 1); |
| } |
| |
| } |
| |
| public final static class InfixLevel extends InBracketPosition { |
| |
| public InfixLevel(final IDocument doc, final int offset, final int length, |
| final int sequence) { |
| super(doc, offset, length, sequence); |
| } |
| |
| @Override |
| public char getOpenChar() { |
| return '%'; |
| } |
| |
| @Override |
| public char getCloseChar() { |
| return '%'; |
| } |
| |
| @Override |
| public boolean insertCR(final int charOffset) { |
| return false; |
| } |
| |
| @Override |
| public boolean matchesClose(final BracketLevel level, final int offset, final char character) { |
| return (getOffset() + getLength() == offset && getCloseChar() == character); |
| } |
| |
| } |
| |
| |
| public static final InBracketPosition createPosition(final char c, |
| final IDocument document, final int offset, final int length, final int sequence) { |
| switch (c) { |
| case '"': |
| return new StringDPosition(document, offset, length, sequence); |
| case '\'': |
| return new StringSPosition(document, offset, length, sequence); |
| case '`': |
| return new QuotedPosition(document, offset, length, sequence); |
| case '{': |
| return new CurlyBracketPosition(document, offset, length, sequence); |
| case '(': |
| return new RoundBracketPosition(document, offset, length, sequence); |
| case '[': |
| return new SquareBracketPosition(document, offset, length, sequence); |
| case '%': |
| return new InfixLevel(document, offset, length, sequence); |
| } |
| throw new IllegalArgumentException(); |
| } |
| |
| |
| public RBracketLevel(final LinkedModeModel model, |
| final IDocument document, final DocContentSections documentContentInfo, |
| final InBracketPosition position, |
| final boolean consoleMode, final boolean autoDelete) { |
| this(model, document, documentContentInfo, |
| ImCollections.<LinkedPosition>newList(position), |
| ((consoleMode) ? CONSOLE_MODE : 0) | ((autoDelete) ? AUTODELETE : 0)); |
| } |
| |
| public RBracketLevel(final LinkedModeModel model, |
| final IDocument document, final DocContentSections documentContentInfo, |
| final List<LinkedPosition> positions, final int mode) { |
| super(model, document, documentContentInfo, positions, mode); |
| } |
| |
| } |