allow to open IDEA diff when IDEA is running (IDEA-67894)

This commit is contained in:
Dmitry Jemerov
2011-04-11 16:36:10 +02:00
parent 2b2cedf561
commit 055dcf6972
4 changed files with 81 additions and 13 deletions
@@ -15,14 +15,20 @@
*/
package com.intellij.openapi.application;
import com.intellij.ExtensionPoints;
import com.intellij.openapi.extensions.ExtensionPointName;
import org.jetbrains.annotations.NonNls;
/**
* Implementors of this interface declared via {@link com.intellij.ExtensionPoints#APPLICATION_STARTER} contribute a
* command line application based on IDEA platform.
*
* @author max
* @see ApplicationStarterEx
*/
public interface ApplicationStarter {
ExtensionPointName<ApplicationStarter> EP_NAME = ExtensionPointName.create(ExtensionPoints.APPLICATION_STARTER);
/**
* Command line switch to start with this runner. For example return "inspect" if you'd like to start app with
* <code>idea.exe inspect</code> cmdline.
@@ -0,0 +1,26 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.application;
/**
* Implementors of this interface declared via {@link com.intellij.ExtensionPoints#APPLICATION_STARTER} are also capable of processing
* an external command line within a running IntelliJ Platform instance.
* @author yole
*/
public interface ApplicationStarterEx extends ApplicationStarter {
void processExternalCommandLine(String[] args);
}
@@ -15,8 +15,10 @@
*/
package com.intellij.openapi.diff;
import com.intellij.openapi.application.ApplicationStarter;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.application.ApplicationStarterEx;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
@@ -26,7 +28,7 @@ import java.io.FileNotFoundException;
/**
* @author max
*/
public class DiffApplication implements ApplicationStarter {
public class DiffApplication implements ApplicationStarterEx {
public String getCommandName() {
return "diff";
}
@@ -37,21 +39,14 @@ public class DiffApplication implements ApplicationStarter {
}
}
private void printHelp() {
private static void printHelp() {
System.err.println(DiffBundle.message("diff.application.usage.parameters.and.description"));
System.exit(1);
}
public void main(String[] args) {
try {
String path1 = args[1];
String path2 = args[2];
VirtualFile file1 = findFile(path1);
VirtualFile file2 = findFile(path2);
SimpleDiffRequest request = SimpleDiffRequest.compareFiles(file1, file2, null);
request.addHint(DiffTool.HINT_SHOW_MODAL_DIALOG);
DiffManager.getInstance().getIdeaDiffTool().show(request);
FileDocumentManager.getInstance().saveAllDocuments();
processDiffCommand(args);
}
catch (FileNotFoundException e) {
System.err.println(e.getMessage());
@@ -65,7 +60,34 @@ public class DiffApplication implements ApplicationStarter {
}
}
private VirtualFile findFile(final String path1) throws FileNotFoundException {
@Override
public void processExternalCommandLine(String[] args) {
if (args.length != 3) {
String productName = ApplicationNamesInfo.getInstance().getProductName();
Messages.showMessageDialog("Usage: " + productName.toLowerCase() + " diff <file1> <file2>",
"Diff", Messages.getInformationIcon());
return;
}
try {
processDiffCommand(args);
}
catch(Exception e) {
Messages.showMessageDialog("Error showing diff: " + e.getMessage(), "Diff", Messages.getErrorIcon());
}
}
private static void processDiffCommand(String[] args) throws FileNotFoundException {
String path1 = args[1];
String path2 = args[2];
VirtualFile file1 = findFile(path1);
VirtualFile file2 = findFile(path2);
SimpleDiffRequest request = SimpleDiffRequest.compareFiles(file1, file2, null);
request.addHint(DiffTool.HINT_SHOW_MODAL_DIALOG);
DiffManager.getInstance().getIdeaDiffTool().show(request);
FileDocumentManager.getInstance().saveAllDocuments();
}
private static VirtualFile findFile(final String path1) throws FileNotFoundException {
final VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(new File(path1));
if (vFile == null) {
throw new FileNotFoundException(DiffBundle.message("cannot.file.file.error.message", path1));
@@ -18,7 +18,10 @@ package com.intellij.ide;
import com.intellij.ide.highlighter.ProjectFileType;
import com.intellij.ide.impl.ProjectUtil;
import com.intellij.idea.StartupUtil;
import com.intellij.openapi.application.ApplicationStarter;
import com.intellij.openapi.application.ApplicationStarterEx;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
@@ -28,6 +31,7 @@ import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.platform.PlatformProjectOpenProcessor;
import com.intellij.projectImport.ProjectOpenProcessor;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
@@ -118,7 +122,17 @@ public class CommandLineProcessor {
}
}
LOG.info("-----");
// TODO[yole] handle AppStarters here?
if (args.size() > 0) {
String command = args.get(0);
for(ApplicationStarter starter: Extensions.getExtensions(ApplicationStarter.EP_NAME)) {
if (starter instanceof ApplicationStarterEx && command.equals(starter.getCommandName())) {
((ApplicationStarterEx) starter).processExternalCommandLine(ArrayUtil.toStringArray(args));
return null;
}
}
}
Project lastOpenedProject = null;
int line = -1;
for (int i = 0, argsSize = args.size(); i < argsSize; i++) {