Skip to content

Commit

Permalink
#319 - Inform about editing ignored file
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz committed Feb 15, 2017
1 parent 1f42d87 commit cb4c2b5
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 32 deletions.
6 changes: 4 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@
language="Ignore"
implementationClass="mobi.hsz.idea.gitignore.codeInsight.SyntaxCompletionContributor"/>

<editorNotificationProvider
implementation="mobi.hsz.idea.gitignore.daemon.MissingGitignoreNotificationProvider"/>
<editorNotificationProvider
implementation="mobi.hsz.idea.gitignore.daemon.AddUnversionedFilesNotificationProvider"/>
<editorNotificationProvider
implementation="mobi.hsz.idea.gitignore.daemon.IgnoredEditingNotificationProvider"/>
<editorNotificationProvider
implementation="mobi.hsz.idea.gitignore.daemon.MissingGitignoreNotificationProvider"/>

<fileTypeFactory
implementation="mobi.hsz.idea.gitignore.file.IgnoreFileTypeFactory"/>
Expand Down
3 changes: 3 additions & 0 deletions resources/messages/IgnoreBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ quick.fix.relative.entry=Remove relative part
quick.fix.syntax.entry=Suggest correct value

daemon.lineMarker.directory=Directory entry
daemon.ignoredEditing=You are editing file which is ignored
daemon.missingGitignore=Missing .gitignore file in GIT project
daemon.missingGitignore.create=Create .gitignore
daemon.addUnversionedFiles=Would you like to add unversioned files to the .gitignore file?
daemon.addUnversionedFiles.create=Add unversioned files
daemon.ok=OK
daemon.cancel=Cancel

dialog.generator.title=Ignore File Generator
Expand Down Expand Up @@ -117,6 +119,7 @@ settings.general.insertAtCursor=Insert new ignore entries at cursor &position
settings.general.addUnversionedFiles=Suggest to add &unversioned files (Git only)
settings.general.unignoreFiles=Enable unignore files group (add entries prefixed with !)
settings.general.informTrackedIgnored=Inform about ignored files that are still tracked (Git only)
settings.general.notifyIgnoredEditing=Inform about editing ignored file
settings.general.donate=Donate me with:
settings.userTemplates=User templates
settings.userTemplates.noTemplateSelected=No template is selected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
public class AddUnversionedFilesNotificationProvider extends EditorNotifications.Provider<EditorNotificationPanel> {
/** Notification key. */
@NotNull
private static final Key<EditorNotificationPanel> KEY =
Key.create(IgnoreBundle.message("daemon.missingGitignore.create"));
private static final Key<EditorNotificationPanel> KEY = Key.create("AddUnversionedFilesNotificationProvider");

/** Current project. */
@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 hsz Jakub Chrzanowski <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package mobi.hsz.idea.gitignore.daemon;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.EditorNotifications;
import mobi.hsz.idea.gitignore.IgnoreBundle;
import mobi.hsz.idea.gitignore.IgnoreManager;
import mobi.hsz.idea.gitignore.settings.IgnoreSettings;
import mobi.hsz.idea.gitignore.util.Icons;
import mobi.hsz.idea.gitignore.util.Properties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Editor notification provider that informs about the attempt of the ignored file modification.
*
* @author Jakub Chrzanowski <[email protected]>
* @since 1.8
*/
public class IgnoredEditingNotificationProvider extends EditorNotifications.Provider<EditorNotificationPanel> {
/** Notification key. */
@NotNull
private static final Key<EditorNotificationPanel> KEY = Key.create("IgnoredEditingNotificationProvider");

/** Current project. */
@NotNull
private final Project project;

/** Notifications component. */
@NotNull
private final EditorNotifications notifications;

/** Plugin settings holder. */
@NotNull
private final IgnoreSettings settings;

/** {@link IgnoreManager} instance. */
@NotNull
private final IgnoreManager manager;

/**
* Builds a new instance of {@link IgnoredEditingNotificationProvider}.
*
* @param project current project
* @param notifications notifications component
*/
public IgnoredEditingNotificationProvider(@NotNull Project project, @NotNull EditorNotifications notifications) {
this.project = project;
this.notifications = notifications;
this.settings = IgnoreSettings.getInstance();
this.manager = IgnoreManager.getInstance(project);
}

/**
* Gets notification key.
*
* @return notification key
*/
@NotNull
@Override
public Key<EditorNotificationPanel> getKey() {
return KEY;
}

/**
* Creates notification panel for given file and checks if is allowed to show the notification.
*
* @param file current file
* @param fileEditor current file editor
* @return created notification panel
*/
@Nullable
@Override
public EditorNotificationPanel createNotificationPanel(@NotNull final VirtualFile file,
@NotNull FileEditor fileEditor) {
if (!settings.isNotifyIgnoredEditing() || !manager.isFileIgnored(file) ||
Properties.isDismissedIgnoredEditingNotification(project, file)) {
return null;
}

final EditorNotificationPanel panel = new EditorNotificationPanel();

panel.setText(IgnoreBundle.message("daemon.ignoredEditing"));
panel.createActionLabel(IgnoreBundle.message("daemon.ok"), new Runnable() {
@Override
public void run() {
Properties.setDismissedIgnoredEditingNotification(project, file);
notifications.updateAllNotifications();
}
});

try { // ignore if older SDK does not support panel icon
panel.icon(Icons.IGNORE);
} catch (NoSuchMethodError ignored) {
}

return panel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
public class MissingGitignoreNotificationProvider extends EditorNotifications.Provider<EditorNotificationPanel> {
/** Notification key. */
@NotNull
private static final Key<EditorNotificationPanel> KEY =
Key.create(IgnoreBundle.message("daemon.missingGitignore.create"));
private static final Key<EditorNotificationPanel> KEY = Key.create("MissingGitignoreNotificationProvider");

/** Current project. */
@NotNull
Expand Down
35 changes: 32 additions & 3 deletions src/mobi/hsz/idea/gitignore/settings/IgnoreSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public enum KEY {
OUTER_IGNORE_RULES("outerIgnoreRules"), OUTER_IGNORE_WRAPPER_HEIGHT("outerIgnoreWrapperHeight"),
INSERT_AT_CURSOR("insertAtCursor"), ADD_UNVERSIONED_FILES("addUnversionedFiles"), VERSION("version"),
STARRED_TEMPLATES("starredTemplates"), UNIGNORE_ACTIONS("unignoreActions"),
HIDE_IGNORED_FILES("hideIgnoredFiles"), INFORM_TRACKED_IGNORED("informTrackedIgnored");
HIDE_IGNORED_FILES("hideIgnoredFiles"), INFORM_TRACKED_IGNORED("informTrackedIgnored"),
NOTIFY_IGNORED_EDITING("notifyIgnoredEditing");

private final String key;

Expand Down Expand Up @@ -113,6 +114,9 @@ public String toString() {
/** Inform user about the tracked and ignored files in the project. */
private boolean informTrackedIgnored = true;

/** Shows notification about editing ignored file. */
private boolean notifyIgnoredEditing = true;

/** Starred templates. */
@NotNull
private final List<String> starredTemplates = ContainerUtil.newArrayList();
Expand Down Expand Up @@ -164,6 +168,7 @@ public Element getState() {
element.setAttribute(KEY.UNIGNORE_ACTIONS.toString(), Boolean.toString(unignoreActions));
element.setAttribute(KEY.HIDE_IGNORED_FILES.toString(), Boolean.toString(hideIgnoredFiles));
element.setAttribute(KEY.INFORM_TRACKED_IGNORED.toString(), Boolean.toString(informTrackedIgnored));
element.setAttribute(KEY.NOTIFY_IGNORED_EDITING.toString(), Boolean.toString(notifyIgnoredEditing));

Element languagesElement = new Element(KEY.LANGUAGES.toString());
for (Map.Entry<IgnoreLanguage, TreeMap<IgnoreLanguagesSettings.KEY, Object>> entry :
Expand Down Expand Up @@ -252,6 +257,11 @@ public void loadState(Element element) {
informTrackedIgnored = Boolean.parseBoolean(value);
}

value = element.getAttributeValue(KEY.NOTIFY_IGNORED_EDITING.toString());
if (value != null) {
notifyIgnoredEditing = Boolean.parseBoolean(value);
}

Element languagesElement = element.getChild(KEY.LANGUAGES.toString());
if (languagesElement != null) {
for (Element languageElement : languagesElement.getChildren()) {
Expand Down Expand Up @@ -430,15 +440,34 @@ public boolean isInformTrackedIgnored() {
}

/**
* Sets value for informing user about the tracked and ignored files in the project.
* Sets notification about editing ignored file status to enabled or disabled.
*
* @param informTrackedIgnored inform about files
* @param informTrackedIgnored show or hide notification
*/
public void setInformTrackedIgnored(boolean informTrackedIgnored) {
this.notifyOnChange(KEY.INFORM_TRACKED_IGNORED, this.informTrackedIgnored, informTrackedIgnored);
this.informTrackedIgnored = informTrackedIgnored;
}

/**
* Checks if notifications about editing ignored file are enabled
*
* @return true if notification are enabled
*/
public boolean isNotifyIgnoredEditing() {
return notifyIgnoredEditing;
}

/**
* Sets value for informing user about the tracked and ignored files in the project.
*
* @param notifyIgnoredEditing inform about files
*/
public void setNotifyIgnoredEditing(boolean notifyIgnoredEditing) {
this.notifyOnChange(KEY.NOTIFY_IGNORED_EDITING, this.notifyIgnoredEditing, notifyIgnoredEditing);
this.notifyIgnoredEditing = notifyIgnoredEditing;
}

/**
* Returns the height of the outer ignore file wrapper panel.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public boolean isModified() {
|| !Comparing.equal(settings.isAddUnversionedFiles(), settingsPanel.isAddUnversionedFiles())
|| !Comparing.equal(settings.isUnignoreActions(), settingsPanel.isUnignoreActions())
|| !Comparing.equal(settings.isInformTrackedIgnored(), settingsPanel.isInformTrackedIgnored())
|| !Comparing.equal(settings.isNotifyIgnoredEditing(), settingsPanel.isNotifyIgnoredEditing())
|| !settingsPanel.getLanguagesSettings().equalSettings(settings.getLanguagesSettings());
}

Expand All @@ -126,6 +127,7 @@ public void apply() throws ConfigurationException {
settings.setLanguagesSettings(settingsPanel.getLanguagesSettings().getSettings());
settings.setUnignoreActions(settingsPanel.isUnignoreActions());
settings.setInformTrackedIgnored(settingsPanel.isInformTrackedIgnored());
settings.setNotifyIgnoredEditing(settingsPanel.isNotifyIgnoredEditing());
}

/** Load settings from other components to configurable. */
Expand All @@ -142,6 +144,7 @@ public void reset() {
settingsPanel.setAddUnversionedFiles(settings.isAddUnversionedFiles());
settingsPanel.setUnignoreActions(settings.isUnignoreActions());
settingsPanel.setInformTrackedIgnored(settings.isInformTrackedIgnored());
settingsPanel.setNotifyIgnoredEditing(settings.isNotifyIgnoredEditing());

IgnoreSettingsPanel.LanguagesTableModel model = settingsPanel.getLanguagesSettings();
model.update(settings.getLanguagesSettings().clone());
Expand Down
21 changes: 10 additions & 11 deletions src/mobi/hsz/idea/gitignore/ui/IgnoreSettingsPanel.form
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="1618" height="400"/>
<xy x="20" y="20" width="1618" height="401"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b1a6e" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="b1a6e" layout-manager="GridLayoutManager" row-count="8" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="true"/>
</constraints>
<properties>
<enabled value="true"/>
</properties>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="none" title-resource-bundle="messages/IgnoreBundle" title-key="settings.general"/>
<children>
<component id="7e9b" class="javax.swing.JCheckBox" binding="missingGitignore">
Expand Down Expand Up @@ -77,6 +74,14 @@
<text resource-bundle="messages/IgnoreBundle" key="settings.general.informTrackedIgnored"/>
</properties>
</component>
<component id="ebecf" class="javax.swing.JCheckBox" binding="notifyIgnoredEditing">
<constraints>
<grid row="7" 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 resource-bundle="messages/IgnoreBundle" key="settings.general.notifyIgnoredEditing"/>
</properties>
</component>
</children>
</grid>
<grid id="24ddc" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
Expand All @@ -85,9 +90,6 @@
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="none" title-resource-bundle="messages/IgnoreBundle" title-key="settings.userTemplates"/>
<children>
<component id="85fc4" class="com.intellij.openapi.ui.Splitter" binding="templatesSplitter" custom-create="true">
Expand All @@ -104,9 +106,6 @@
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="none" title-resource-bundle="messages/IgnoreBundle" title-key="settings.languagesSettings"/>
<children>
<scrollpane id="9895d" class="com.intellij.ui.components.JBScrollPane" binding="languagesPanel" custom-create="true">
Expand Down
21 changes: 21 additions & 0 deletions src/mobi/hsz/idea/gitignore/ui/IgnoreSettingsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public class IgnoreSettingsPanel implements Disposable {
/** Panel with information about donations. */
private JPanel donatePanel;

/** Inform about editing ignored file. */
private JCheckBox notifyIgnoredEditing;

/** Editor panel element. */
private EditorPanel editorPanel;

Expand Down Expand Up @@ -342,6 +345,24 @@ public void setInformTrackedIgnored(boolean selected) {
this.informTrackedIgnored.setSelected(selected);
}

/**
* Returns value of @{link {@link #notifyIgnoredEditing}} field.
*
* @return {@link #notifyIgnoredEditing} is selected
*/
public boolean isNotifyIgnoredEditing() {
return notifyIgnoredEditing.isSelected();
}

/**
* Sets value of {@link #notifyIgnoredEditing} field.
*
* @param selected value for {@link #notifyIgnoredEditing}
*/
public void setNotifyIgnoredEditing(boolean selected) {
this.notifyIgnoredEditing.setSelected(selected);
}

/**
* Returns model of {@link #languagesTable}.
*
Expand Down
Loading

0 comments on commit cb4c2b5

Please sign in to comment.