/** * Validates that content of the editor as well as caret and selection matches one specified in data file that * should be formed with the same format as one used in configureByFile * @param message - this check specific message. Added to text, caret position, selection checking. May be null * @param filePath - relative path from %IDEA_INSTALLATION_HOME%/testData/ * @param ignoreTrailingSpaces - whether trailing spaces in editor in data file should be stripped prior to comparing. */ protected void checkResultByFile(@Nullable String message, @TestDataFile @NotNull String filePath, final boolean ignoreTrailingSpaces) { bringRealEditorBack(); getProject().getComponent(PostprocessReformattingAspect.class).doPostponedFormatting(); if (ignoreTrailingSpaces) { final Editor editor = myEditor; TrailingSpacesStripper.stripIfNotCurrentLine(editor.getDocument(), false); EditorUtil.fillVirtualSpaceUntilCaret(editor); } PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); String fullPath = getTestDataPath() + filePath; File ioFile = new File(fullPath); assertTrue(getMessage("Cannot find file " + fullPath, message), ioFile.exists()); String fileText = null; try { fileText = FileUtil.loadFile(ioFile, CharsetToolkit.UTF8_CHARSET); } catch (IOException e) { LOG.error(e); } checkResultByText(message, StringUtil.convertLineSeparators(fileText), ignoreTrailingSpaces, getTestDataPath() + "/" + filePath); }
@Override public void saveDocumentAsIs(@NotNull Document document) { VirtualFile file = getFile(document); boolean spaceStrippingEnabled = true; if (file != null) { spaceStrippingEnabled = TrailingSpacesStripper.isEnabled(file); TrailingSpacesStripper.setEnabled(file, false); } try { saveDocument(document); } finally { if (file != null) { TrailingSpacesStripper.setEnabled(file, spaceStrippingEnabled); } } }
public void testReparseDoesNotModifyDocument() throws Exception { VirtualFile file = createTempFile("txt", null, "1\n2\n3\n", Charset.forName("UTF-8")); file.putUserData(TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY, EditorSettingsExternalizable.STRIP_TRAILING_SPACES_CHANGED); final Document document = FileDocumentManager.getInstance().getDocument(file); assertNotNull(document); WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { @Override public void run() { document.insertString(document.getTextLength(), " "); } }); PsiDocumentManager.getInstance(myProject).reparseFiles(Collections.singleton(file), false); assertEquals("1\n2\n3\n ", VfsUtilCore.loadText(file)); WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { @Override public void run() { document.insertString(0, "-"); } }); FileDocumentManager.getInstance().saveDocument(document); assertEquals("-1\n2\n3\n", VfsUtilCore.loadText(file)); }
private void applySettings(VirtualFile file) { if (file == null) return; if (!Utils.isEnabled(CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings())) return; // Get editorconfig settings final String filePath = Utils.getFilePath(myProject, file); final SettingsProviderComponent settingsProvider = SettingsProviderComponent.getInstance(); final List<EditorConfig.OutPair> outPairs = settingsProvider.getOutPairs(myProject, filePath); // Apply trailing spaces setting final String trimTrailingWhitespace = Utils.configValueForKey(outPairs, trimTrailingWhitespaceKey); applyConfigValueToUserData(file, TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY, trimTrailingWhitespaceKey, trimTrailingWhitespace, trimMap); // Apply final newline setting final String insertFinalNewline = Utils.configValueForKey(outPairs, insertFinalNewlineKey); applyConfigValueToUserData(file, TrailingSpacesStripper.OVERRIDE_ENSURE_NEWLINE_KEY, insertFinalNewlineKey, insertFinalNewline, newlineMap); }
/** * Validates that content of the editor as well as caret and selection matches one specified in data file that * should be formed with the same format as one used in configureByFile * @param message - this check specific message. Added to text, caret position, selection checking. May be null * @param filePath - relative path from %IDEA_INSTALLATION_HOME%/testData/ * @param ignoreTrailingSpaces - whether trailing spaces in editor in data file should be stripped prior to comparing. * @throws Exception */ protected void checkResultByFile(@Nullable String message, @Nonnull String filePath, final boolean ignoreTrailingSpaces) { bringRealEditorBack(); getProject().getComponent(PostprocessReformattingAspect.class).doPostponedFormatting(); if (ignoreTrailingSpaces) { final Editor editor = myEditor; TrailingSpacesStripper.strip(editor.getDocument(), false, true); EditorUtil.fillVirtualSpaceUntilCaret(editor); } PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); String fullPath = getTestDataPath() + filePath; File ioFile = new File(fullPath); assertTrue(getMessage("Cannot find file " + fullPath, message), ioFile.exists()); String fileText = null; try { fileText = FileUtil.loadFile(ioFile, CharsetToolkit.UTF8); } catch (IOException e) { LOG.error(e); } checkResultByText(message, StringUtil.convertLineSeparators(fileText), ignoreTrailingSpaces, getTestDataPath() + "/" + filePath); }
@Override public void saveDocumentAsIs(@Nonnull Document document) { VirtualFile file = getFile(document); boolean spaceStrippingEnabled = true; if (file != null) { spaceStrippingEnabled = TrailingSpacesStripper.isEnabled(file); TrailingSpacesStripper.setEnabled(file, false); } try { saveDocument(document); } finally { if (file != null) { TrailingSpacesStripper.setEnabled(file, spaceStrippingEnabled); } } }
public void testSave() { doTest("<div> \n \n</div><caret>", "\n", "<div> \n \n</div>\n"); final PsiFile file = myFixture.getFile(); myFixture.getEditor().getCaretModel().moveToOffset(myFixture.getDocument(file).getTextLength() - 2); file.getVirtualFile().putUserData(TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY, EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE); FileDocumentManager.getInstance().saveDocument(myFixture.getDocument(file)); myFixture.checkResult("<div>\n\n</div>\n"); }
private static void applySettings(VirtualFile file) { if (file == null || !file.isInLocalFileSystem()) return; // Get editorconfig settings final String filePath = file.getCanonicalPath(); final SettingsProviderComponent settingsProvider = SettingsProviderComponent.getInstance(); final List<EditorConfig.OutPair> outPairs = settingsProvider.getOutPairs(filePath); // Apply trailing spaces setting final String trimTrailingWhitespace = Utils.configValueForKey(outPairs, trimTrailingWhitespaceKey); applyConfigValueToUserData(file, TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY, trimTrailingWhitespaceKey, trimTrailingWhitespace, trimMap); // Apply final newline setting final String insertFinalNewline = Utils.configValueForKey(outPairs, insertFinalNewlineKey); applyConfigValueToUserData(file, TrailingSpacesStripper.OVERRIDE_ENSURE_NEWLINE_KEY, insertFinalNewlineKey, insertFinalNewline, newlineMap); }
private void performAction() { EditorActionManager actionManager = EditorActionManager.getInstance(); EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_ENTER); actionHandler.execute(myEditor, DataManager.getInstance().getDataContext()); TrailingSpacesStripper.strip(myEditor.getDocument(), false, false); }