Skip to content

Commit

Permalink
Merge pull request #80 from SeeSharpSoft/fb_enforce_quoting_setting
Browse files Browse the repository at this point in the history
[FEATURE] table-editor: setting to toggle enforced value quoting
  • Loading branch information
SeeSharpSoft committed Dec 20, 2018
2 parents ce9e4f7 + fc585a6 commit 945b76c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/main/java/net/seesharpsoft/intellij/plugins/csv/CsvHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,19 @@ public static String unquoteCsvValue(String content) {
return result;
}

public static String quoteCsvField(String content) {
private static boolean isQuotingRequired(String content, String separator) {
return content != null && (content.contains(separator) || content.contains("\"") || content.contains("\n"));
}

public static String quoteCsvField(String content, String separator, boolean quotingEnforced) {
if (content == null) {
return "";
}
String result = content.replaceAll("\"", "\"\"");
return "\"" + result + "\"";
if (quotingEnforced || isQuotingRequired(content, separator)) {
String result = content.replaceAll("\"", "\"\"");
return "\"" + result + "\"";
}
return content;
}

public static <T> T[][] deepCopy(T[][] matrix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static final class OptionSet {
public int TABLE_EDITOR_ROW_HEIGHT;

public boolean SHOW_TABLE_EDITOR_INFO_PANEL;
public boolean QUOTING_ENFORCED;

public OptionSet() {
EditorSettingsExternalizable editorSettingsExternalizable = EditorSettingsExternalizable.getInstance();
Expand All @@ -52,6 +53,7 @@ public OptionSet() {
EDITOR_PRIO = EditorPrio.TEXT_FIRST;
SHOW_TABLE_EDITOR_INFO_PANEL = true;
TABLE_EDITOR_ROW_HEIGHT = TABLE_EDITOR_DEFAULT_ROW_HEIGHT;
QUOTING_ENFORCED = false;
}
}

Expand Down Expand Up @@ -155,4 +157,11 @@ public void setTableEditorRowHeight(int rowHeight) {
if (finalRowHeight < TABLE_EDITOR_MIN_ROW_HEIGHT) finalRowHeight = TABLE_EDITOR_MIN_ROW_HEIGHT;
getState().TABLE_EDITOR_ROW_HEIGHT = finalRowHeight;
}

public boolean isQuotingEnforced() {
return getState().QUOTING_ENFORCED;
}
public void setQuotingEnforced(boolean quotingEnforced) {
getState().QUOTING_ENFORCED = quotingEnforced;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
</grid>
</children>
</grid>
<grid id="3e325" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="3e325" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand Down Expand Up @@ -140,6 +140,14 @@
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="d6a8e" class="javax.swing.JCheckBox" binding="cbQuotingEnforced">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Quote every value (even if not needed)"/>
</properties>
</component>
</children>
</grid>
<vspacer id="ff292">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CsvEditorSettingsProvider implements SearchableConfigurable {
private JCheckBox cbShowInfoPanel;
private JComboBox cbRowHeight;
private JComboBox cbEditorUsage;
private JCheckBox cbQuotingEnforced;

@NotNull
@Override
Expand Down Expand Up @@ -62,7 +63,8 @@ public boolean isModified() {
cbTabHighlightColor.isSelected() != csvEditorSettingsExternalizable.isHighlightTabSeparator() ||
!Objects.equals(cbTabHighlightColor.getColor(), csvEditorSettingsExternalizable.getTabHighlightColor()) ||
!Objects.equals(cbRowHeight.getSelectedIndex(), csvEditorSettingsExternalizable.getTableEditorRowHeight()) ||
!Objects.equals(cbEditorUsage.getSelectedIndex(), csvEditorSettingsExternalizable.getEditorPrio().ordinal());
!Objects.equals(cbEditorUsage.getSelectedIndex(), csvEditorSettingsExternalizable.getEditorPrio().ordinal()) ||
isModified(cbQuotingEnforced, csvEditorSettingsExternalizable.isQuotingEnforced());
}

@Override
Expand All @@ -77,6 +79,7 @@ public void reset() {
cbTabHighlightColor.setColor(csvEditorSettingsExternalizable.getTabHighlightColor());
cbRowHeight.setSelectedIndex(csvEditorSettingsExternalizable.getTableEditorRowHeight());
cbEditorUsage.setSelectedIndex(csvEditorSettingsExternalizable.getEditorPrio().ordinal());
cbQuotingEnforced.setSelected(csvEditorSettingsExternalizable.isQuotingEnforced());
}

@Override
Expand All @@ -91,6 +94,7 @@ public void apply() throws ConfigurationException {
csvEditorSettingsExternalizable.setTabHighlightColor(cbTabHighlightColor.getColor());
csvEditorSettingsExternalizable.setTableEditorRowHeight(cbRowHeight.getSelectedIndex());
csvEditorSettingsExternalizable.setEditorPrio(CsvEditorSettingsExternalizable.EditorPrio.values()[cbEditorUsage.getSelectedIndex()]);
csvEditorSettingsExternalizable.setQuotingEnforced(cbQuotingEnforced.isSelected());
}

protected void createUIComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.intellij.psi.PsiFile;
import net.seesharpsoft.intellij.plugins.csv.CsvColumnInfoMap;
import net.seesharpsoft.intellij.plugins.csv.CsvHelper;
import net.seesharpsoft.intellij.plugins.csv.editor.CsvEditorSettingsExternalizable;
import net.seesharpsoft.intellij.plugins.csv.editor.table.api.TableDataHandler;
import net.seesharpsoft.intellij.plugins.csv.psi.CsvFile;
import net.seesharpsoft.intellij.plugins.csv.settings.CsvCodeStyleSettings;
Expand Down Expand Up @@ -132,7 +133,7 @@ protected String sanitizeFieldValue(Object value) {
if (value == null) {
return "";
}
return CsvHelper.quoteCsvField(value.toString());
return CsvHelper.quoteCsvField(value.toString(), this.currentSeparator, CsvEditorSettingsExternalizable.getInstance().isQuotingEnforced());
}

protected String generateCsv(Object[][] data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void testResetAndModified() throws ConfigurationException {
csvEditorSettingsExternalizable.setHighlightTabSeparator(false);
csvEditorSettingsExternalizable.setShowInfoBalloon(false);
csvEditorSettingsExternalizable.setTabHighlightColor(Color.BLACK);
csvEditorSettingsExternalizable.setQuotingEnforced(true);

assertEquals(true, editorSettingsPanel.isModified());

Expand All @@ -73,6 +74,7 @@ public void testResetAndModified() throws ConfigurationException {
assertEquals(false, csvEditorSettingsExternalizable.isHighlightTabSeparator());
assertEquals(false, csvEditorSettingsExternalizable.isShowInfoBalloon());
assertEquals(Color.BLACK, csvEditorSettingsExternalizable.getTabHighlightColor());
assertEquals(true, csvEditorSettingsExternalizable.isQuotingEnforced());

editorSettingsPanel.disposeUIResources();
}
Expand All @@ -89,6 +91,7 @@ public void testApply() throws ConfigurationException {
csvEditorSettingsExternalizable.setHighlightTabSeparator(false);
csvEditorSettingsExternalizable.setShowInfoBalloon(false);
csvEditorSettingsExternalizable.setTabHighlightColor(Color.BLACK);
csvEditorSettingsExternalizable.setQuotingEnforced(true);

editorSettingsPanel.apply();

Expand All @@ -101,6 +104,7 @@ public void testApply() throws ConfigurationException {
assertEquals(freshOptionSet.HIGHTLIGHT_TAB_SEPARATOR, csvEditorSettingsExternalizable.isHighlightTabSeparator());
assertEquals(freshOptionSet.SHOW_INFO_BALLOON, csvEditorSettingsExternalizable.isShowInfoBalloon());
assertEquals(freshOptionSet.TAB_HIGHLIGHT_COLOR, "" + csvEditorSettingsExternalizable.getTabHighlightColor().getRGB());
assertEquals(freshOptionSet.QUOTING_ENFORCED, csvEditorSettingsExternalizable.isQuotingEnforced());

editorSettingsPanel.disposeUIResources();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.ide.structureView.StructureViewBuilder;
import com.intellij.openapi.fileEditor.FileEditorStateLevel;
import com.intellij.openapi.util.Key;
import net.seesharpsoft.intellij.plugins.csv.editor.CsvEditorSettingsExternalizable;
import net.seesharpsoft.intellij.plugins.csv.editor.table.CsvTableEditor;
import net.seesharpsoft.intellij.plugins.csv.editor.table.CsvTableEditorState;

Expand Down Expand Up @@ -108,4 +109,15 @@ public void testTableCsvGeneration() throws FileNotFoundException {
assertEquals(expectedContent, generatedCsv);
}

public void testTableCsvGenerationEnforceQuoting() throws FileNotFoundException {
changeValue("new value", 2, 1);
CsvEditorSettingsExternalizable.getInstance().setQuotingEnforced(true);
String generatedCsv = fileEditor.generateCsv(fileEditor.getDataHandler().getCurrentState());

File resultFile = new File(this.getTestDataPath(), "TableEditorFileChangedQuoted.csv");
String expectedContent = new BufferedReader(new FileReader(resultFile)).lines().reduce(null, (prev, line) -> prev == null ? line : prev + "\n" + line);

assertEquals(expectedContent, generatedCsv);
}

}
8 changes: 4 additions & 4 deletions src/test/resources/editor/TableEditorFileChanged.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Header1","header 2"
"this is column ""Header1""","this is column header 2"
"just another line with leading and trailing whitespaces","new value"
"",
Header1,header 2
"this is column ""Header1""",this is column header 2
just another line with leading and trailing whitespaces,new value
,
4 changes: 4 additions & 0 deletions src/test/resources/editor/TableEditorFileChangedQuoted.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"Header1","header 2"
"this is column ""Header1""","this is column header 2"
"just another line with leading and trailing whitespaces","new value"
"",

0 comments on commit 945b76c

Please sign in to comment.