package net.seesharpsoft.intellij.plugins.csv.editor;

import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorPolicy;
import com.intellij.openapi.fileEditor.FileEditorProvider;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;

public class CsvFileEditorTest extends LightCodeInsightFixtureTestCase {

    @Override
    protected String getTestDataPath() {
        return "./src/test/resources/editor";
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testCsvFileEditorProviderIsAvailableAndHasCorrectNameAndPolicy() {
        myFixture.configureByFiles("AnyFile.csv");

        FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(myFixture.getProject(), myFixture.getFile().getVirtualFile());
        assertEquals(1, fileEditorProviders.length);
        assertInstanceOf(fileEditorProviders[0], CsvFileEditorProvider.class);

        FileEditorProvider fileEditorProvider = fileEditorProviders[0];
        assertEquals("csv-text-editor", fileEditorProvider.getEditorTypeId());
        assertEquals(FileEditorPolicy.HIDE_DEFAULT_EDITOR, fileEditorProvider.getPolicy());
    }

    private void disposeTextEditor(FileEditor fileEditor) {
        FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(myFixture.getProject(), myFixture.getFile().getVirtualFile());
        fileEditorProviders[0].disposeEditor(fileEditor);
    }

    public void testCsvEditorIsTextEditorWithInitialCsvEditorSettings() {
        myFixture.configureByFiles("AnyFile.csv");

        FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(myFixture.getProject(), myFixture.getFile().getVirtualFile());
        FileEditor fileEditor = fileEditorProviders[0].createEditor(myFixture.getProject(), myFixture.getFile().getVirtualFile());
        assertInstanceOf(fileEditor, TextEditor.class);

        TextEditor textEditor = (TextEditor)fileEditor;

        CsvEditorSettingsExternalizable csvEditorSettingsExternalizable = CsvEditorSettingsExternalizable.getInstance();
        EditorSettings editorSettings = textEditor.getEditor().getSettings();
        assertEquals(csvEditorSettingsExternalizable.isCaretRowShown(), editorSettings.isCaretRowShown());
        assertEquals(csvEditorSettingsExternalizable.isUseSoftWraps(), editorSettings.isUseSoftWraps());

        disposeTextEditor(textEditor);
    }

    private TextEditor getCurrentTextEditor() {
        FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(myFixture.getProject(), myFixture.getFile().getVirtualFile());
        return (TextEditor)fileEditorProviders[0].createEditor(myFixture.getProject(), myFixture.getFile().getVirtualFile());
    }

    public void testCsvEditorSettingsAreApplied() {
        myFixture.configureByFiles("AnyFile.csv");

        CsvEditorSettingsExternalizable csvEditorSettingsExternalizable = CsvEditorSettingsExternalizable.getInstance();
        csvEditorSettingsExternalizable.setCaretRowShown(false);
        csvEditorSettingsExternalizable.setUseSoftWraps(true);

        TextEditor textEditor = getCurrentTextEditor();

        EditorSettings editorSettings = textEditor.getEditor().getSettings();
        assertEquals(csvEditorSettingsExternalizable.isCaretRowShown(), editorSettings.isCaretRowShown());
        assertEquals(csvEditorSettingsExternalizable.isUseSoftWraps(), editorSettings.isUseSoftWraps());

        disposeTextEditor(textEditor);
    }

}

@@ -48,16 +48,73 @@
 ditor;%0A%0A
+import com.intellij.ide.impl.convert.JDomConvertingUtil;%0A
 import c
@@ -199,75 +199,9 @@
 tor.
-FileEditor;%0Aimport com.intellij.openapi.fileEditor.FileEditorPolicy
+*
 ;%0Aim
@@ -237,16 +237,19 @@
 eEditor.
+ex.
 FileEdit
@@ -258,16 +258,23 @@
 Provider
+Manager
 ;%0Aimport
@@ -306,16 +306,26 @@
 eEditor.
+impl.text.
 TextEdit
@@ -326,16 +326,21 @@
 xtEditor
+State
 ;%0Aimport
@@ -365,47 +365,21 @@
 api.
-fileEditor.ex.FileEditorProviderManager
+util.JDOMUtil
 ;%0Aim
@@ -451,16 +451,84 @@
 estCase;
+%0Aimport org.jetbrains.jps.model.serialization.JDomSerializationUtil;
 %0A%0Apublic
@@ -785,16 +785,68 @@
 setUp();
+%0A%0A        myFixture.configureByFiles(%22AnyFile.csv%22);
 %0A    %7D%0A%0A
@@ -932,60 +932,8 @@
 ) %7B%0A
-        myFixture.configureByFiles(%22AnyFile.csv%22);%0A%0A
     
@@ -1848,60 +1848,8 @@
 ) %7B%0A
-        myFixture.configureByFiles(%22AnyFile.csv%22);%0A%0A
     
@@ -3118,60 +3118,8 @@
 ) %7B%0A
-        myFixture.configureByFiles(%22AnyFile.csv%22);%0A%0A
     
@@ -3757,11 +3757,895 @@
 ;%0A    %7D%0A
+    %0A    public void testCsvEditorStateReadsAndWritesStates() %7B%0A        TextEditor textEditor = getCurrentTextEditor();%0A%0A        FileEditorProvider%5B%5D fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(myFixture.getProject(), myFixture.getFile().getVirtualFile());%0A        CsvFileEditorProvider fileEditorProvider = (CsvFileEditorProvider)fileEditorProviders%5B0%5D;%0A%0A        FileEditorState state = fileEditorProvider.readState(JDomConvertingUtil.createComponentElement(JDomSerializationUtil.COMPONENT_ELEMENT), this.getProject(), this.getFile().getVirtualFile());%0A        assertInstanceOf(state, TextEditorState.class);%0A        textEditor.setState(state);%0A        fileEditorProvider.writeState(state, this.getProject(), JDomConvertingUtil.createComponentElement(JDomSerializationUtil.COMPONENT_ELEMENT));%0A        %0A        disposeTextEditor(textEditor);%0A    %7D%0A    
 %0A%7D%0A

test added