Skip to content

Commit

Permalink
Fixed JetBrains#17 Prevent adding duplicate entries
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz authored and zolotov committed Jul 25, 2014
1 parent f226e5b commit dccae55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/messages/GitignoreBundle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
action.addToGitignore=Add to .gitignore
action.addToGitignore.description=Add this file to .gitignore rules
action.appendFile.entryExists=Entry "{0}" already exists
action.newFile=.gitignore
action.newFile.description=Create new .gitignore file
action.newFile.exists=Gitignore file already exists
Expand Down
16 changes: 16 additions & 0 deletions src/mobi/hsz/idea/gitignore/command/AppendFileCommandAction.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package mobi.hsz.idea.gitignore.command;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import mobi.hsz.idea.gitignore.GitignoreBundle;
import mobi.hsz.idea.gitignore.GitignoreLanguage;
import mobi.hsz.idea.gitignore.util.Utils;
import org.jetbrains.annotations.NotNull;

public class AppendFileCommandAction extends WriteCommandAction<PsiFile> {
Expand All @@ -27,6 +34,15 @@ protected void run(@NotNull Result<PsiFile> result) throws Throwable {
}
Document document = PsiDocumentManager.getInstance(project).getDocument(file);
if (document != null) {
for (PsiElement element : file.getChildren()) {
if (content.equals(element.getText())) {
Notifications.Bus.notify(new Notification(GitignoreLanguage.NAME,
GitignoreBundle.message("action.appendFile.entryExists", content),
"in " + Utils.getRelativePath(project.getBaseDir(), file.getVirtualFile()),
NotificationType.WARNING), project);
return;
}
}
document.insertString(document.getTextLength(), "\n" + content.replace("\r", ""));
PsiDocumentManager.getInstance(project).commitDocument(document);
}
Expand Down

0 comments on commit dccae55

Please sign in to comment.