-
Notifications
You must be signed in to change notification settings - Fork 19
Add configurable file path navigation for HOCON string values. #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||
| package org.jetbrains.plugins.hocon | ||||||||
| package ref | ||||||||
|
|
||||||||
| import com.intellij.openapi.module.ModuleManager | ||||||||
| import com.intellij.openapi.roots.ModuleRootManager | ||||||||
| import com.intellij.openapi.util.TextRange | ||||||||
| import com.intellij.psi.* | ||||||||
| import com.intellij.util.ProcessingContext | ||||||||
| import org.jetbrains.jps.model.java.JavaResourceRootType | ||||||||
| import org.jetbrains.plugins.hocon.psi.HStringValue | ||||||||
| import org.jetbrains.plugins.hocon.settings.HoconProjectSettings | ||||||||
|
|
||||||||
| class HoconFilePathReferenceProvider extends PsiReferenceProvider { | ||||||||
| def getReferencesByElement(element: PsiElement, context: ProcessingContext): Array[PsiReference] = | ||||||||
| element match { | ||||||||
| case hstr: HStringValue => | ||||||||
| val project = element.getProject | ||||||||
| val settings = HoconProjectSettings.getInstance(project) | ||||||||
| val extensions = settings.fileNavigationExtensionsList | ||||||||
| if (extensions.isEmpty) PsiReference.EMPTY_ARRAY | ||||||||
| else { | ||||||||
| val filePath = hstr.stringValue | ||||||||
| if (!extensions.exists(ext => filePath.endsWith("." + ext))) PsiReference.EMPTY_ARRAY | ||||||||
| else { | ||||||||
| val range = ElementManipulators.getValueTextRange(element) | ||||||||
| Array(new HoconFilePathReference(filePath, element, range, settings)) | ||||||||
| } | ||||||||
| } | ||||||||
| case _ => PsiReference.EMPTY_ARRAY | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| class HoconFilePathReference( | ||||||||
| filePath: String, | ||||||||
| element: PsiElement, | ||||||||
| range: TextRange, | ||||||||
| settings: HoconProjectSettings, | ||||||||
| ) extends PsiReferenceBase[PsiElement](element, range) { | ||||||||
|
|
||||||||
| def resolve(): PsiElement = { | ||||||||
| val project = element.getProject | ||||||||
| val searchRoots = settings.fileNavigationSearchRootsList | ||||||||
| val psiManager = PsiManager.getInstance(project) | ||||||||
| val modules = ModuleManager.getInstance(project).getModules | ||||||||
|
|
||||||||
| val result = modules.iterator.flatMap { module => | ||||||||
| val mrm = ModuleRootManager.getInstance(module) | ||||||||
|
|
||||||||
| val baseDirs = if (searchRoots.nonEmpty) { | ||||||||
| // Use explicitly configured roots (relative paths from content roots) | ||||||||
| mrm.getContentRoots.iterator.flatMap { contentRoot => | ||||||||
| searchRoots.iterator.flatMap { searchRoot => | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
| val dir = if (searchRoot.isEmpty) contentRoot else contentRoot.findFileByRelativePath(searchRoot) | ||||||||
| Option(dir).iterator | ||||||||
|
Comment on lines
+53
to
+54
|
||||||||
| val dir = if (searchRoot.isEmpty) contentRoot else contentRoot.findFileByRelativePath(searchRoot) | |
| Option(dir).iterator | |
| Option(contentRoot.findFileByRelativePath(searchRoot)).iterator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use for comprehension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use for comprehension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be val
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,5 +14,6 @@ class HoconReferenceContributor extends PsiReferenceContributor { | |
| def registerReferenceProviders(registrar: PsiReferenceRegistrar): Unit = { | ||
| registrar.registerReferenceProvider(pattern[HIncludeTarget], new IncludedFileReferenceProvider) | ||
| registrar.registerReferenceProvider(pattern[HStringValue], new HoconPropertiesReferenceProvider) | ||
| registrar.registerReferenceProvider(pattern[HStringValue], new HoconFilePathReferenceProvider) | ||
| } | ||
|
Comment on lines
14
to
18
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,6 +27,16 @@ class HoconProjectSettings extends PersistentStateComponent[HoconProjectSettings | |||||
| @BeanProperty var classReferencesOnQuotedStrings = true | ||||||
| @BeanProperty var propertyReferencesOnStrings = true | ||||||
| @BeanProperty var searchInGotoSymbol = false | ||||||
| @BeanProperty var fileNavigationExtensions: String = "" | ||||||
| @BeanProperty var fileNavigationSearchRoots: String = "" | ||||||
|
|
||||||
| def fileNavigationExtensionsList: List[String] = | ||||||
| if (fileNavigationExtensions.trim.isEmpty) Nil | ||||||
| else fileNavigationExtensions.split(",").map(_.trim).filter(_.nonEmpty).toList | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplication of logic here and in fileNavigationSearchRootsList |
||||||
|
|
||||||
|
Comment on lines
+33
to
+36
|
||||||
| def fileNavigationSearchRootsList: List[String] = | ||||||
| if (fileNavigationSearchRoots.trim.isEmpty) Nil | ||||||
| else fileNavigationSearchRoots.split(",").map(_.trim).filter(_.nonEmpty).toList | ||||||
|
||||||
| else fileNavigationSearchRoots.split(",").map(_.trim).filter(_.nonEmpty).toList | |
| else fileNavigationSearchRoots.split(",").map(_.trim).toList |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,34 +12,69 @@ public class HoconProjectSettingsPanel { | |
| private final Project project; | ||
|
|
||
| private JPanel mainPanel; | ||
| private JPanel wrapperPanel; | ||
| private JCheckBox classReferencesUnquotedCheckBox; | ||
| private JCheckBox classReferencesQuotedCheckBox; | ||
| private JCheckBox propertyReferencesCheckBox; | ||
| private JCheckBox searchInGotoSymbol; | ||
| private JTextField fileNavigationExtensionsField; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use JBList - you won't have to parse the strings and it will look better |
||
| private JTextField fileNavigationSearchRootsField; | ||
|
|
||
| public HoconProjectSettingsPanel(Project project) { | ||
| this.project = project; | ||
| buildWrapperPanel(); | ||
| loadSettings(); | ||
| } | ||
|
|
||
| private void buildWrapperPanel() { | ||
| fileNavigationExtensionsField = new JTextField(); | ||
| fileNavigationSearchRootsField = new JTextField(); | ||
|
|
||
| JPanel fileNavPanel = new JPanel(new GridBagLayout()); | ||
| fileNavPanel.setBorder(BorderFactory.createTitledBorder( | ||
| BorderFactory.createEtchedBorder(), "File Path Navigation")); | ||
|
|
||
| GridBagConstraints gbc = new GridBagConstraints(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use FormBuilder instead of manual Grid |
||
| gbc.anchor = GridBagConstraints.WEST; | ||
| gbc.insets = new Insets(2, 4, 2, 4); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just use |
||
|
|
||
| gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; | ||
| fileNavPanel.add(new JLabel("File extensions to navigate (comma-separated, e.g. sql,xml):"), gbc); | ||
| gbc.gridx = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; | ||
| fileNavPanel.add(fileNavigationExtensionsField, gbc); | ||
|
|
||
| gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; | ||
| fileNavPanel.add(new JLabel("Search root directories (comma-separated, default: resource directory from project model):"), gbc); | ||
| gbc.gridx = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; | ||
| fileNavPanel.add(fileNavigationSearchRootsField, gbc); | ||
|
|
||
| wrapperPanel = new JPanel(new BorderLayout()); | ||
| wrapperPanel.add(mainPanel, BorderLayout.NORTH); | ||
| wrapperPanel.add(fileNavPanel, BorderLayout.CENTER); | ||
| } | ||
|
|
||
| public void loadSettings() { | ||
| HoconProjectSettings settings = HoconProjectSettings.getInstance(project); | ||
| classReferencesUnquotedCheckBox.setSelected(settings.getClassReferencesOnUnquotedStrings()); | ||
| classReferencesQuotedCheckBox.setSelected(settings.getClassReferencesOnQuotedStrings()); | ||
| propertyReferencesCheckBox.setSelected(settings.getPropertyReferencesOnStrings()); | ||
| searchInGotoSymbol.setSelected(settings.getSearchInGotoSymbol()); | ||
| fileNavigationExtensionsField.setText(settings.getFileNavigationExtensions()); | ||
| fileNavigationSearchRootsField.setText(settings.getFileNavigationSearchRoots()); | ||
| } | ||
|
|
||
| public JComponent getMainComponent() { | ||
| return mainPanel; | ||
| return wrapperPanel; | ||
| } | ||
|
|
||
| public boolean isModified() { | ||
| HoconProjectSettings settings = HoconProjectSettings.getInstance(project); | ||
| return classReferencesUnquotedCheckBox.isSelected() != settings.getClassReferencesOnUnquotedStrings() || | ||
| classReferencesQuotedCheckBox.isSelected() != settings.getClassReferencesOnQuotedStrings() || | ||
| propertyReferencesCheckBox.isSelected() != settings.getPropertyReferencesOnStrings() || | ||
| searchInGotoSymbol.isSelected() != settings.getSearchInGotoSymbol(); | ||
| searchInGotoSymbol.isSelected() != settings.getSearchInGotoSymbol() || | ||
| !fileNavigationExtensionsField.getText().equals(settings.getFileNavigationExtensions()) || | ||
| !fileNavigationSearchRootsField.getText().equals(settings.getFileNavigationSearchRoots()); | ||
| } | ||
|
|
||
| public void apply() { | ||
|
|
@@ -48,6 +83,8 @@ public void apply() { | |
| settings.setClassReferencesOnQuotedStrings(classReferencesQuotedCheckBox.isSelected()); | ||
| settings.setPropertyReferencesOnStrings(propertyReferencesCheckBox.isSelected()); | ||
| settings.setSearchInGotoSymbol(searchInGotoSymbol.isSelected()); | ||
| settings.setFileNavigationExtensions(fileNavigationExtensionsField.getText()); | ||
| settings.setFileNavigationSearchRoots(fileNavigationSearchRootsField.getText()); | ||
| } | ||
|
|
||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use for comprehension