use of com.intellij.openapi.editor.ex.EditorSettingsExternalizable in project intellij-community by JetBrains.
the class TrailingSpacesStripperTest method testDoNotStripModifiedLines_And_EnsureBlankLineAtTheEnd_LeavesWhitespacesAtTheEndOfFileAlone.
public void testDoNotStripModifiedLines_And_EnsureBlankLineAtTheEnd_LeavesWhitespacesAtTheEndOfFileAlone() throws IOException {
EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
settings.setStripTrailingSpaces(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
settings.setEnsureNewLineAtEOF(true);
Document document = configureFromFileText("x.txt", "xxx <caret>\nyyy\n\t\t\t");
// make any modification, so that Document and file content differ. Otherwise save won't be, and "on-save" actions won't be called.
WriteCommandAction.runWriteCommandAction(getProject(), () -> document.insertString(0, " "));
FileDocumentManager.getInstance().saveAllDocuments();
checkResultByText(" xxx <caret>\nyyy\n\t\t\t\n");
}
use of com.intellij.openapi.editor.ex.EditorSettingsExternalizable in project intellij-community by JetBrains.
the class TrailingSpacesStripperTest method testModifySameLineInTwoFilesAndSaveAllShouldStripAtLeastOneFile.
public void testModifySameLineInTwoFilesAndSaveAllShouldStripAtLeastOneFile() throws IOException {
EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
settings.setStripTrailingSpaces(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_CHANGED);
Editor editor1 = createHeavyEditor("x1.txt", "x11 <caret>\nyyy\n");
Editor editor2 = createHeavyEditor("x2.txt", "x22 <caret>\nyyy\n");
type(' ', editor1, getProject());
type(' ', editor2, getProject());
FileDocumentManager.getInstance().saveAllDocuments();
assertEquals("x11\nyyy\n", editor1.getDocument().getText());
// caret in the way in second but not in the first
assertEquals("x22 \nyyy\n", editor2.getDocument().getText());
}
use of com.intellij.openapi.editor.ex.EditorSettingsExternalizable in project intellij-community by JetBrains.
the class TrailingSpacesStripper method strip.
private void strip(@NotNull final Document document) {
if (!document.isWritable())
return;
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
VirtualFile file = fileDocumentManager.getFile(document);
if (file == null || !file.isValid() || Boolean.TRUE.equals(DISABLE_FOR_FILE_KEY.get(file)))
return;
final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
if (settings == null)
return;
final String overrideStripTrailingSpacesData = file.getUserData(OVERRIDE_STRIP_TRAILING_SPACES_KEY);
final Boolean overrideEnsureNewlineData = file.getUserData(OVERRIDE_ENSURE_NEWLINE_KEY);
@EditorSettingsExternalizable.StripTrailingSpaces String stripTrailingSpaces = overrideStripTrailingSpacesData != null ? overrideStripTrailingSpacesData : settings.getStripTrailingSpaces();
final boolean doStrip = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
final boolean ensureEOL = overrideEnsureNewlineData != null ? overrideEnsureNewlineData.booleanValue() : settings.isEnsureNewLineAtEOF();
if (doStrip) {
final boolean inChangedLinesOnly = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
boolean success = strip(document, inChangedLinesOnly, settings.isKeepTrailingSpacesOnCaretLine());
if (!success) {
myDocumentsToStripLater.add(document);
}
}
final int lines = document.getLineCount();
if (ensureEOL && lines > 0) {
final int start = document.getLineStartOffset(lines - 1);
final int end = document.getLineEndOffset(lines - 1);
if (start != end) {
final CharSequence content = document.getCharsSequence();
ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) {
@Override
public void run() {
CommandProcessor.getInstance().runUndoTransparentAction(() -> {
if (CharArrayUtil.containsOnlyWhiteSpaces(content.subSequence(start, end)) && doStrip) {
document.deleteString(start, end);
} else {
document.insertString(end, "\n");
}
});
}
});
}
}
}
use of com.intellij.openapi.editor.ex.EditorSettingsExternalizable in project intellij-community by JetBrains.
the class TrailingSpacesStripper method clearLineModificationFlags.
// clears line modification flags except lines which was not stripped because the caret was in the way
public void clearLineModificationFlags(@NotNull Document document) {
if (document instanceof DocumentWindow) {
document = ((DocumentWindow) document).getDelegate();
}
if (!(document instanceof DocumentImpl)) {
return;
}
Editor activeEditor = getActiveEditor(document);
// when virtual space enabled, we can strip whitespace anywhere
boolean isVirtualSpaceEnabled = activeEditor == null || activeEditor.getSettings().isVirtualSpace();
final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
if (settings == null)
return;
boolean enabled = !Boolean.TRUE.equals(DISABLE_FOR_FILE_KEY.get(FileDocumentManager.getInstance().getFile(document)));
if (!enabled)
return;
String stripTrailingSpaces = settings.getStripTrailingSpaces();
final boolean doStrip = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
final boolean inChangedLinesOnly = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
int[] caretLines;
if (activeEditor != null && inChangedLinesOnly && doStrip && !isVirtualSpaceEnabled) {
List<Caret> carets = activeEditor.getCaretModel().getAllCarets();
caretLines = new int[carets.size()];
for (int i = 0; i < carets.size(); i++) {
Caret caret = carets.get(i);
caretLines[i] = caret.getLogicalPosition().line;
}
} else {
caretLines = ArrayUtil.EMPTY_INT_ARRAY;
}
((DocumentImpl) document).clearLineModificationFlagsExcept(caretLines);
}
use of com.intellij.openapi.editor.ex.EditorSettingsExternalizable in project intellij-community by JetBrains.
the class PyEditingTest method testGreedyBackspace.
public void testGreedyBackspace() {
// PY-254
final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
boolean oldVSpaceValue = settings.isVirtualSpace();
try {
settings.setVirtualSpace(true);
doTestBackspace("py254", new LogicalPosition(4, 8));
} finally {
settings.setVirtualSpace(oldVSpaceValue);
}
}
Aggregations