IDEA-158519 Setting to override default text direction for mixed LTR/RTL text

This commit is contained in:
Dmitry Batrak
2016-07-15 13:45:45 +03:00
parent b055b0a278
commit d3bf00a8c2
7 changed files with 138 additions and 18 deletions
@@ -0,0 +1,61 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.editor.actions;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.ToggleAction;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.editor.ex.BidiTextDirection;
public abstract class SetEditorBidiTextDirectionAction extends ToggleAction {
private final BidiTextDirection myDirection;
private SetEditorBidiTextDirectionAction(BidiTextDirection direction) {
myDirection = direction;
}
@Override
public boolean isSelected(AnActionEvent e) {
return EditorSettingsExternalizable.getInstance().getBidiTextDirection() == myDirection;
}
@Override
public void setSelected(AnActionEvent e, boolean state) {
if (myDirection != EditorSettingsExternalizable.getInstance().getBidiTextDirection()) {
EditorSettingsExternalizable.getInstance().setBidiTextDirection(myDirection);
EditorFactory.getInstance().refreshAllEditors();
}
}
public static class ContentBased extends SetEditorBidiTextDirectionAction {
public ContentBased() {
super(BidiTextDirection.CONTENT_BASED);
}
}
public static class Ltr extends SetEditorBidiTextDirectionAction {
public Ltr() {
super(BidiTextDirection.LTR);
}
}
public static class Rtl extends SetEditorBidiTextDirectionAction {
public Rtl() {
super(BidiTextDirection.RTL);
}
}
}
@@ -0,0 +1,20 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.editor.ex;
public enum BidiTextDirection {
CONTENT_BASED, LTR, RTL
}
@@ -84,6 +84,8 @@ public class EditorSettingsExternalizable implements PersistentStateComponent<Ed
public boolean SHOW_NOTIFICATION_AFTER_OPTIMIZE_IMPORTS_ACTION = true;
public boolean ADD_CARETS_ON_DOUBLE_CTRL = true;
public BidiTextDirection BIDI_TEXT_DIRECTION = BidiTextDirection.CONTENT_BASED;
}
private static final String COMPOSITE_PROPERTY_SEPARATOR = ":";
@@ -524,4 +526,12 @@ public class EditorSettingsExternalizable implements PersistentStateComponent<Ed
public void setAddCaretsOnDoubleCtrl(boolean val) {
myOptions.ADD_CARETS_ON_DOUBLE_CTRL = val;
}
public BidiTextDirection getBidiTextDirection() {
return myOptions.BIDI_TEXT_DIRECTION;
}
public void setBidiTextDirection(BidiTextDirection direction) {
myOptions.BIDI_TEXT_DIRECTION = direction;
}
}
@@ -23,6 +23,7 @@ import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.VisualPosition;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.editor.impl.FontInfo;
@@ -36,6 +37,7 @@ import org.jetbrains.annotations.TestOnly;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.text.Bidi;
/**
* A facade for components responsible for drawing editor contents, managing editor size
@@ -59,6 +61,7 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable {
private String myPrefixText; // accessed only in EDT
private LineLayout myPrefixLayout; // guarded by myLock
private TextAttributes myPrefixAttributes; // accessed only in EDT
private int myBidiFlags; // accessed only in EDT
private int myPlainSpaceWidth; // guarded by myLock
private int myLineHeight; // guarded by myLock
@@ -287,6 +290,16 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable {
myPlainSpaceWidth = -1;
myTabSize = -1;
}
switch (EditorSettingsExternalizable.getInstance().getBidiTextDirection()) {
case LTR:
myBidiFlags = Bidi.DIRECTION_LEFT_TO_RIGHT;
break;
case RTL:
myBidiFlags = Bidi.DIRECTION_RIGHT_TO_LEFT;
break;
default:
myBidiFlags = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
}
setFontRenderContext();
myLogicalPositionCache.reset(false);
myTextLayoutCache.resetToDocumentSize(false);
@@ -489,6 +502,10 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable {
Insets getInsets() {
return myEditor.getContentComponent().getInsets();
}
int getBidiFlags() {
return myBidiFlags;
}
private static void assertIsDispatchThread() {
ApplicationManager.getApplication().assertIsDispatchThread();
@@ -22,7 +22,6 @@ import com.intellij.openapi.editor.bidi.LanguageBidiRegionsSeparator;
import com.intellij.openapi.editor.colors.FontPreferences;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.impl.ComplementaryFontsRegistry;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.editor.impl.FontInfo;
import com.intellij.psi.StringEscapesTokenTypes;
import com.intellij.psi.tree.IElementType;
@@ -95,25 +94,23 @@ abstract class LineLayout {
}
private static List<BidiRun> createFragments(@NotNull EditorView view, int line, boolean skipBidiLayout) {
EditorImpl editor = view.getEditor();
Document document = editor.getDocument();
Document document = view.getEditor().getDocument();
int lineStartOffset = document.getLineStartOffset(line);
int lineEndOffset = document.getLineEndOffset(line);
if (lineEndOffset <= lineStartOffset) return Collections.emptyList();
if (skipBidiLayout) return Collections.singletonList(new BidiRun(lineEndOffset - lineStartOffset));
CharSequence text = document.getImmutableCharSequence().subSequence(lineStartOffset, lineEndOffset);
char[] chars = CharArrayUtil.fromSequence(text);
return createRuns(editor, chars, lineStartOffset);
return createRuns(view, chars, lineStartOffset);
}
private static List<BidiRun> createFragments(@NotNull EditorView view, @NotNull CharSequence text,
@JdkConstants.FontStyle int fontStyle) {
if (text.length() == 0) return Collections.emptyList();
EditorImpl editor = view.getEditor();
FontRenderContext fontRenderContext = view.getFontRenderContext();
FontPreferences fontPreferences = editor.getColorsScheme().getFontPreferences();
FontPreferences fontPreferences = view.getEditor().getColorsScheme().getFontPreferences();
char[] chars = CharArrayUtil.fromSequence(text);
List<BidiRun> runs = createRuns(editor, chars, -1);
List<BidiRun> runs = createRuns(view, chars, -1);
for (BidiRun run : runs) {
for (Chunk chunk : run.getChunks()) {
chunk.fragments = new ArrayList<>();
@@ -123,32 +120,33 @@ abstract class LineLayout {
return runs;
}
private static List<BidiRun> createRuns(EditorImpl editor, char[] text, int startOffsetInEditor) {
private static List<BidiRun> createRuns(EditorView view, char[] text, int startOffsetInEditor) {
int textLength = text.length;
if (editor.myDisableRtl || !Bidi.requiresBidi(text, 0, textLength)) {
if (view.getEditor().myDisableRtl || !Bidi.requiresBidi(text, 0, textLength)) {
return Collections.singletonList(new BidiRun(textLength));
}
List<BidiRun> runs = new ArrayList<BidiRun>();
int flags = view.getBidiFlags();
if (startOffsetInEditor >= 0) {
// running bidi algorithm separately for text fragments corresponding to different lexer tokens
int lastOffset = startOffsetInEditor;
IElementType lastToken = null;
HighlighterIterator iterator = editor.getHighlighter().createIterator(startOffsetInEditor);
HighlighterIterator iterator = view.getEditor().getHighlighter().createIterator(startOffsetInEditor);
int endOffsetInEditor = startOffsetInEditor + textLength;
while (!iterator.atEnd() && iterator.getStart() < endOffsetInEditor) {
IElementType currentToken = iterator.getTokenType();
if (distinctTokens(lastToken, currentToken)) {
int tokenStart = Math.max(iterator.getStart(), startOffsetInEditor);
addRuns(runs, text, lastOffset - startOffsetInEditor, tokenStart - startOffsetInEditor);
addRuns(runs, text, lastOffset - startOffsetInEditor, tokenStart - startOffsetInEditor, flags);
lastToken = currentToken;
lastOffset = tokenStart;
}
iterator.advance();
}
addRuns(runs, text, lastOffset - startOffsetInEditor, endOffsetInEditor - startOffsetInEditor);
addRuns(runs, text, lastOffset - startOffsetInEditor, endOffsetInEditor - startOffsetInEditor, flags);
}
else {
addRuns(runs, text, 0, textLength);
addRuns(runs, text, 0, textLength, flags);
}
return runs;
}
@@ -163,21 +161,21 @@ abstract class LineLayout {
return separator.createBorderBetweenTokens(token1, token2);
}
private static void addRuns(List<BidiRun> runs, char[] text, int start, int end) {
private static void addRuns(List<BidiRun> runs, char[] text, int start, int end, int flags) {
int afterLastTabPosition = start;
for (int i = start; i < end; i++) {
if (text[i] == '\t') {
addRunsNoTabs(runs, text, afterLastTabPosition, i);
addRunsNoTabs(runs, text, afterLastTabPosition, i, flags);
afterLastTabPosition = i + 1;
addOrMergeRun(runs, new BidiRun((byte)0, i, i + 1));
}
}
addRunsNoTabs(runs, text, afterLastTabPosition, end);
addRunsNoTabs(runs, text, afterLastTabPosition, end, flags);
}
private static void addRunsNoTabs(List<BidiRun> runs, char[] text, int start, int end) {
private static void addRunsNoTabs(List<BidiRun> runs, char[] text, int start, int end, int flags) {
if (start >= end) return;
Bidi bidi = new Bidi(text, start, null, 0, end - start, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
Bidi bidi = new Bidi(text, start, null, 0, end - start, flags);
int runCount = bidi.getRunCount();
for (int i = 0; i < runCount; i++) {
addOrMergeRun(runs, new BidiRun((byte)bidi.getRunLevel(i), start + bidi.getRunStart(i), start + bidi.getRunLimit(i)));
@@ -169,6 +169,12 @@ action.EditorDuplicate.text=Duplicate Line or Selection
action.EditorDuplicateLines.text=Duplicate Entire Lines
action.EditorSelectWord.text=Extend Selection
action.EditorUnSelectWord.text=Shrink Selection
group.EditorBidiTextDirection.text=Bidi Text Direction
group.EditorBidiTextDirection.description=Primary direction for mixed LTR/RTL text
action.EditorSetContentBasedBidiTextDirection.text=Content-based
action.EditorSetContentBasedBidiTextDirection.description=Direction is determined by initial character(s)
action.EditorSetLtrBidiTextDirection.text=Left-to-Right
action.EditorSetRtlBidiTextDirection.text=Right-to-Left
action.EditorToggleInsertState.text=Toggle Insert/Overwrite
action.EditorToggleColumnMode.text=Column Selection _Mode
action.EditorToggleColumnMode.description=Toggle column selection mode in current editor
@@ -371,6 +371,14 @@
<action id="EditorToggleUseSoftWraps" class="com.intellij.openapi.editor.actions.ToggleUseSoftWrapsMenuAction" icon="AllIcons.Actions.ToggleSoftWrap"/>
</group>
<separator/>
<group id="EditorBidiTextDirection" popup="true">
<action id="EditorSetContentBasedBidiTextDirection" class="com.intellij.openapi.editor.actions.SetEditorBidiTextDirectionAction$ContentBased"/>
<action id="EditorSetLtrBidiTextDirection" class="com.intellij.openapi.editor.actions.SetEditorBidiTextDirectionAction$Ltr"/>
<action id="EditorSetRtlBidiTextDirection" class="com.intellij.openapi.editor.actions.SetEditorBidiTextDirectionAction$Rtl"/>
</group>
<separator/>
<!-- Mac OS Lion: In native apps toggle full screen mode is the last action in "View" menu -->
<group id="ToggleFullScreenGroup">
<separator/>