Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
481 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions
19
src/main/java/net/seesharpsoft/intellij/lang/FileParserDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package net.seesharpsoft.intellij.lang; | ||
|
||
import com.intellij.lang.ParserDefinition; | ||
import com.intellij.lang.PsiParser; | ||
import com.intellij.lexer.Lexer; | ||
import com.intellij.psi.PsiFile; | ||
|
||
/** | ||
* Support for file specific parser definition. | ||
*/ | ||
public interface FileParserDefinition extends ParserDefinition { | ||
default Lexer createLexer(PsiFile file) { | ||
return createLexer(file.getProject()); | ||
} | ||
|
||
default PsiParser createParser(PsiFile file) { | ||
return createParser(file.getProject()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/net/seesharpsoft/intellij/plugins/csv/CsvStorageHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package net.seesharpsoft.intellij.plugins.csv; | ||
|
||
import com.intellij.openapi.util.Key; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.PathUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public final class CsvStorageHelper { | ||
public static final String CSV_STATE_STORAGE_FILE = "csv-plugin.xml"; | ||
|
||
public static final Key<String> RELATIVE_FILE_URL = Key.create("CSV_PLUGIN_RELATIVE_URL"); | ||
|
||
public static String getRelativeFileUrl(@NotNull PsiFile psiFile) { | ||
String url = psiFile.getUserData(RELATIVE_FILE_URL); | ||
if (url == null) { | ||
String projectDir = PathUtil.getLocalPath(psiFile.getProject().getBasePath()); | ||
url = PathUtil.getLocalPath(psiFile.getOriginalFile().getVirtualFile().getPath()) | ||
.replaceFirst("^" + Pattern.quote(projectDir), ""); | ||
psiFile.putUserData(RELATIVE_FILE_URL, url); | ||
} | ||
return url; | ||
} | ||
|
||
private CsvStorageHelper() { | ||
// static | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/net/seesharpsoft/intellij/plugins/csv/actions/CsvChangeSeparatorAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package net.seesharpsoft.intellij.plugins.csv.actions; | ||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.actionSystem.ToggleAction; | ||
import com.intellij.openapi.components.ServiceManager; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.FileContentUtil; | ||
import net.seesharpsoft.intellij.plugins.csv.CsvLanguage; | ||
import net.seesharpsoft.intellij.plugins.csv.CsvSeparatorHolder; | ||
import net.seesharpsoft.intellij.plugins.csv.components.CsvFileAttributes; | ||
import net.seesharpsoft.intellij.plugins.csv.settings.CsvCodeStyleSettings; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class CsvChangeSeparatorAction extends ToggleAction { | ||
private String mySeparator; | ||
|
||
CsvChangeSeparatorAction(String separator, String mySeparatorTextArg) { | ||
super(mySeparatorTextArg); | ||
mySeparator = separator; | ||
} | ||
|
||
@Override | ||
public boolean isSelected(@NotNull AnActionEvent anActionEvent) { | ||
PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE); | ||
if (psiFile == null) { | ||
return false; | ||
} | ||
CsvFileAttributes csvFileAttributes = ServiceManager.getService(psiFile.getProject(), CsvFileAttributes.class); | ||
return csvFileAttributes.getFileSeparator(psiFile) != null && CsvCodeStyleSettings.getCurrentSeparator(psiFile).equals(mySeparator); | ||
} | ||
|
||
@Override | ||
public void setSelected(@NotNull AnActionEvent anActionEvent, boolean b) { | ||
PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE); | ||
if (psiFile == null) { | ||
return; | ||
} | ||
Language language = psiFile.getLanguage(); | ||
if (!language.isKindOf(CsvLanguage.INSTANCE) || language instanceof CsvSeparatorHolder) { | ||
return; | ||
} | ||
CsvFileAttributes csvFileAttributes = ServiceManager.getService(psiFile.getProject(), CsvFileAttributes.class); | ||
csvFileAttributes.setFileSeparator(psiFile, this.mySeparator); | ||
FileContentUtil.reparseFiles(psiFile.getVirtualFile()); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ain/java/net/seesharpsoft/intellij/plugins/csv/actions/CsvChangeSeparatorActionGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package net.seesharpsoft.intellij.plugins.csv.actions; | ||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.openapi.actionSystem.ActionGroup; | ||
import com.intellij.openapi.actionSystem.AnAction; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.psi.PsiFile; | ||
import net.seesharpsoft.intellij.plugins.csv.CsvLanguage; | ||
import net.seesharpsoft.intellij.plugins.csv.CsvSeparatorHolder; | ||
import net.seesharpsoft.intellij.plugins.csv.settings.CsvCodeStyleSettings; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class CsvChangeSeparatorActionGroup extends ActionGroup { | ||
|
||
private static final AnAction[] CSV_SEPARATOR_CHANGE_ACTIONS; | ||
|
||
static { | ||
CSV_SEPARATOR_CHANGE_ACTIONS = new AnAction[CsvCodeStyleSettings.SUPPORTED_SEPARATORS.length + 1]; | ||
for (int i = 0; i < CSV_SEPARATOR_CHANGE_ACTIONS.length - 1; ++i) { | ||
CSV_SEPARATOR_CHANGE_ACTIONS[i] = new CsvChangeSeparatorAction(CsvCodeStyleSettings.SUPPORTED_SEPARATORS[i], CsvCodeStyleSettings.SUPPORTED_SEPARATORS_DISPLAY[i]); | ||
} | ||
CSV_SEPARATOR_CHANGE_ACTIONS[CSV_SEPARATOR_CHANGE_ACTIONS.length - 1] = new CsvDefaultSeparatorAction(); | ||
} | ||
|
||
@Override | ||
public void update(AnActionEvent anActionEvent) { | ||
PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE); | ||
Language language = psiFile == null ? null : psiFile.getLanguage(); | ||
anActionEvent.getPresentation().setEnabledAndVisible(psiFile != null && language != null && | ||
language.isKindOf(CsvLanguage.INSTANCE) && !(language instanceof CsvSeparatorHolder) | ||
); | ||
|
||
if (psiFile != null) { | ||
anActionEvent.getPresentation() | ||
.setText(String.format("CSV Value Separator: %s", CsvCodeStyleSettings.getSeparatorDisplayText(CsvCodeStyleSettings.getCurrentSeparator(psiFile)))); | ||
} | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent) { | ||
return CSV_SEPARATOR_CHANGE_ACTIONS; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/net/seesharpsoft/intellij/plugins/csv/actions/CsvDefaultSeparatorAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package net.seesharpsoft.intellij.plugins.csv.actions; | ||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.actionSystem.ToggleAction; | ||
import com.intellij.openapi.components.ServiceManager; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.FileContentUtil; | ||
import net.seesharpsoft.intellij.plugins.csv.CsvLanguage; | ||
import net.seesharpsoft.intellij.plugins.csv.CsvSeparatorHolder; | ||
import net.seesharpsoft.intellij.plugins.csv.components.CsvFileAttributes; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class CsvDefaultSeparatorAction extends ToggleAction { | ||
CsvDefaultSeparatorAction() { | ||
super("Project Default"); | ||
} | ||
|
||
@Override | ||
public boolean isSelected(@NotNull AnActionEvent anActionEvent) { | ||
PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE); | ||
if (psiFile == null) { | ||
return false; | ||
} | ||
CsvFileAttributes csvFileAttributes = ServiceManager.getService(psiFile.getProject(), CsvFileAttributes.class); | ||
return csvFileAttributes.getFileSeparator(psiFile) == null; | ||
} | ||
|
||
@Override | ||
public void setSelected(@NotNull AnActionEvent anActionEvent, boolean b) { | ||
PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE); | ||
if (psiFile == null) { | ||
return; | ||
} | ||
Language language = psiFile.getLanguage(); | ||
if (!language.isKindOf(CsvLanguage.INSTANCE) || language instanceof CsvSeparatorHolder) { | ||
return; | ||
} | ||
CsvFileAttributes csvFileAttributes = ServiceManager.getService(psiFile.getProject(), CsvFileAttributes.class); | ||
csvFileAttributes.removeFileSeparator(psiFile); | ||
FileContentUtil.reparseFiles(psiFile.getVirtualFile()); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/net/seesharpsoft/intellij/plugins/csv/components/CsvFileAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.seesharpsoft.intellij.plugins.csv.components; | ||
|
||
import com.intellij.openapi.components.PersistentStateComponent; | ||
import com.intellij.openapi.components.State; | ||
import com.intellij.openapi.components.Storage; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.xmlb.XmlSerializerUtil; | ||
import net.seesharpsoft.intellij.plugins.csv.CsvStorageHelper; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@State( | ||
name = "CsvFileAttributes", | ||
storages = {@Storage(CsvStorageHelper.CSV_STATE_STORAGE_FILE)} | ||
) | ||
@SuppressWarnings("all") | ||
public class CsvFileAttributes implements PersistentStateComponent<CsvFileAttributes> { | ||
|
||
public Map<String, Attribute> attributeMap = new HashMap<>(); | ||
|
||
static class Attribute { | ||
public String separator; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public CsvFileAttributes getState() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public void loadState(@NotNull CsvFileAttributes state) { | ||
XmlSerializerUtil.copyBean(state, this); | ||
} | ||
|
||
protected String generateMapKey(@NotNull PsiFile psiFile) { | ||
return CsvStorageHelper.getRelativeFileUrl(psiFile); | ||
} | ||
|
||
public void setFileSeparator(@NotNull PsiFile psiFile, @NotNull String separator) { | ||
Attribute state = attributeMap.get(generateMapKey(psiFile)); | ||
if (state == null) { | ||
state = new Attribute(); | ||
attributeMap.put(generateMapKey(psiFile), state); | ||
} | ||
state.separator = separator; | ||
} | ||
|
||
public void removeFileSeparator(@NotNull PsiFile psiFile) { | ||
attributeMap.remove(generateMapKey(psiFile)); | ||
} | ||
|
||
public String getFileSeparator(@NotNull PsiFile psiFile) { | ||
Attribute state = attributeMap.get(generateMapKey(psiFile)); | ||
if (state != null) { | ||
return state.separator; | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.