summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.java
blob: c9a3fec987c97b30aa0d739b9986a7c8d021d89d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ide;

import com.intellij.featureStatistics.fusCollectors.LifecycleUsageTriggerCollector;
import com.intellij.ide.actions.ShowLogAction;
import com.intellij.ide.impl.OpenProjectTask;
import com.intellij.ide.impl.OpenResult;
import com.intellij.ide.impl.ProjectUtil;
import com.intellij.ide.impl.ProjectUtilCore;
import com.intellij.ide.lightEdit.LightEdit;
import com.intellij.ide.lightEdit.LightEditFeatureUsagesUtil;
import com.intellij.ide.lightEdit.LightEditService;
import com.intellij.ide.lightEdit.LightEditUtil;
import com.intellij.ide.util.PsiNavigationSupport;
import com.intellij.idea.CommandLineArgs;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.fileEditor.impl.NonProjectFileWritingAccessProvider;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.platform.CommandLineProjectOpenProcessor;
import com.intellij.platform.PlatformProjectOpenProcessor;
import com.intellij.pom.Navigatable;
import com.intellij.util.PlatformUtils;
import com.intellij.util.containers.ContainerUtil;
import io.netty.handler.codec.http.QueryStringDecoder;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

import static com.intellij.ide.lightEdit.LightEditFeatureUsagesUtil.OpenPlace.CommandLine;
import static com.intellij.util.io.URLUtil.SCHEME_SEPARATOR;

public final class CommandLineProcessor {
  private static final Logger LOG = Logger.getInstance(CommandLineProcessor.class);
  private static final String OPTION_WAIT = "--wait";

  public static final Future<CliResult> OK_FUTURE = CompletableFuture.completedFuture(CliResult.OK);

  @ApiStatus.Internal
  public static final String SCHEME_INTERNAL = "!!!internal!!!";

  private CommandLineProcessor() { }

  // public for testing
  @ApiStatus.Internal
  public static @NotNull CommandLineProcessorResult doOpenFileOrProject(@NotNull Path file, boolean shouldWait) {
    Project project = null;
    if (!LightEditUtil.isForceOpenInLightEditMode()) {
      OpenProjectTask options = PlatformProjectOpenProcessor.createOptionsToOpenDotIdeaOrCreateNewIfNotExists(file, null);
      // do not check for .ipr files in the specified directory (@develar: it is existing behaviour, I am not fully sure that it is correct)
      ProjectUtil.PREVENT_IPR_LOOKUP_KEY.set(options, Boolean.TRUE);
      OpenResult openResult = ProjectUtil.tryOpenOrImport(file, options);
      if (openResult instanceof OpenResult.Success) {
        project = ((OpenResult.Success)openResult).getProject();
      }
      else if (openResult instanceof OpenResult.Cancel) {
        return CommandLineProcessorResult.createError(IdeBundle.message("dialog.message.open.cancelled"));
      }
    }
    if (project == null) {
      return doOpenFile(file, -1, -1, false, shouldWait);
    }
    else {
      return new CommandLineProcessorResult(project, shouldWait ? CommandLineWaitingManager.getInstance().addHookForProject(project) : OK_FUTURE);
    }
  }

  private static CommandLineProcessorResult doOpenFile(Path ioFile, int line, int column, boolean tempProject, boolean shouldWait) {
    Project[] projects = tempProject ? new Project[0] : ProjectUtilCore.getOpenProjects();
    if (!tempProject && projects.length == 0 && PlatformUtils.isDataGrip()) {
      RecentProjectsManager recentProjectManager = RecentProjectsManager.getInstance();
      if (recentProjectManager.willReopenProjectOnStart() && recentProjectManager.reopenLastProjectsOnStart().join()) {
        projects = ProjectUtilCore.getOpenProjects();
      }
    }

    VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(ioFile);
    if (file == null) {
      if (LightEditUtil.isLightEditEnabled()) {
        Project lightEditProject = LightEditUtil.openFile(ioFile, true);
        if (lightEditProject != null) {
          Future<CliResult> future = shouldWait ? CommandLineWaitingManager.getInstance().addHookForPath(ioFile) : OK_FUTURE;
          return new CommandLineProcessorResult(lightEditProject, future);
        }
      }
      return CommandLineProcessorResult.createError(IdeBundle.message("dialog.message.can.not.open.file", ioFile.toString()));
    }

    if (projects.length == 0) {
      Project project = CommandLineProjectOpenProcessor.getInstance().openProjectAndFile(ioFile, line, column, tempProject);
      if (project == null) {
        return CommandLineProcessorResult.createError(IdeBundle.message("dialog.message.no.project.found.to.open.file.in"));
      }

      return new CommandLineProcessorResult(project, shouldWait ? CommandLineWaitingManager.getInstance().addHookForFile(file) : OK_FUTURE);
    }
    else {
      NonProjectFileWritingAccessProvider.allowWriting(Collections.singletonList(file));
      Project project;
      if (LightEditUtil.isForceOpenInLightEditMode()) {
        project = LightEditService.getInstance().openFile(file);
        LightEditFeatureUsagesUtil.logFileOpen(project, CommandLine);
      }
      else {
        project = findBestProject(file, projects);
        Navigatable navigatable = line > 0
                                  ? new OpenFileDescriptor(project, file, line - 1, Math.max(column, 0))
                                  : PsiNavigationSupport.getInstance().createNavigatable(project, file, -1);
        AppUIExecutor.onUiThread().expireWith(project).execute(() -> {
          navigatable.navigate(true);
        });
      }

      return new CommandLineProcessorResult(project, shouldWait ? CommandLineWaitingManager.getInstance().addHookForFile(file) : OK_FUTURE);
    }
  }

  private static Project findBestProject(VirtualFile file, Project[] projects) {
    for (Project project : projects) {
      ProjectFileIndex fileIndex = ProjectFileIndex.getInstance(project);
      if (ReadAction.compute(() -> fileIndex.isInContent(file))) {
        return project;
      }
    }

    IdeFrame frame = IdeFocusManager.getGlobalInstance().getLastFocusedFrame();
    if (frame != null) {
      Project project = frame.getProject();
      if (project != null && !LightEdit.owns(project)) {
        return project;
      }
    }

    return projects[0];
  }

  @ApiStatus.Internal
  public static @NotNull CompletableFuture<CliResult> processProtocolCommand(@NotNull @NlsSafe String rawUri) {
    LOG.info("external URI request:\n" + rawUri);

    if (ApplicationManager.getApplication().isHeadlessEnvironment()) {
      throw new IllegalStateException("cannot process URI requests in headless state");
    }

    boolean internal = rawUri.startsWith(SCHEME_INTERNAL);
    String uri = internal ? rawUri.substring(SCHEME_INTERNAL.length()) : rawUri;
    int separatorStart = uri.indexOf(SCHEME_SEPARATOR);
    if (separatorStart < 0) throw new IllegalArgumentException(uri);

    String scheme = uri.substring(0, separatorStart), query = uri.substring(separatorStart + SCHEME_SEPARATOR.length());
    CompletableFuture<CliResult> result = new CompletableFuture<>();
    ProgressManager.getInstance().run(new Task.Backgroundable(null, IdeBundle.message("ide.protocol.progress.title"), true) {
      @Override
      public void run(@NotNull ProgressIndicator indicator) {
        indicator.setIndeterminate(true);
        indicator.setText(uri);
        (internal ? processInternalProtocol(query) : ProtocolHandler.process(scheme, query, indicator))
          .exceptionally(t -> {
            LOG.error(t);
            return new CliResult(0, IdeBundle.message("ide.protocol.exception", t.getClass().getSimpleName(), t.getMessage()));
          })
          .thenAccept(cliResult -> {
            result.complete(cliResult);
            if (cliResult.message != null) {
              String title = IdeBundle.message("ide.protocol.cannot.title");
              new Notification(Notifications.SYSTEM_MESSAGES_GROUP_ID, title, cliResult.message, NotificationType.WARNING)
                .addAction(ShowLogAction.notificationAction())
                .notify(null);
            }
          });
      }
    });
    return result;
  }

  private static CompletableFuture<CliResult> processInternalProtocol(String query) {
    try {
      QueryStringDecoder decoder = new QueryStringDecoder(query);
      if ("open".equals(decoder.path())) {
        Map<String, List<String>> parameters = decoder.parameters();
        String fileStr = ContainerUtil.getLastItem(parameters.get("file"));
        if (fileStr != null && !fileStr.isBlank()) {
          Path file = parseFilePath(fileStr, null);
          if (file != null) {
            int line = StringUtil.parseInt(ContainerUtil.getLastItem(parameters.get("line")), -1);
            int column = StringUtil.parseInt(ContainerUtil.getLastItem(parameters.get("column")), -1);
            CommandLineProcessorResult result = openFileOrProject(file, line, column, false, false, false);
            LifecycleUsageTriggerCollector.onProtocolOpenCommandHandled(result.getProject());
            return CompletableFuture.completedFuture(CliResult.OK);
          }
        }
      }

      return CompletableFuture.completedFuture(new CliResult(0, IdeBundle.message("ide.protocol.internal.bad.query", query)));
    }
    catch (Throwable t) {
      return CompletableFuture.failedFuture(t);
    }
  }

  public static @NotNull CommandLineProcessorResult processExternalCommandLine(@NotNull List<String> args, @Nullable String currentDirectory) {
    StringBuilder logMessage = new StringBuilder();
    logMessage.append("External command line:").append('\n');
    logMessage.append("Dir: ").append(currentDirectory).append('\n');
    for (String arg : args) {
      logMessage.append(arg).append('\n');
    }
    logMessage.append("-----");
    LOG.info(logMessage.toString());

    if (args.isEmpty()) {
      return new CommandLineProcessorResult(null, OK_FUTURE);
    }

    CommandLineProcessorResult result = processApplicationStarters(args, currentDirectory);
    if (result != null) return result;

    return processOpenFile(args, currentDirectory);
  }

  private static @Nullable CommandLineProcessorResult processApplicationStarters(List<String> args, @Nullable String currentDirectory) {
    String command = args.get(0);
    return ApplicationStarter.EP_NAME.computeSafeIfAny(starter -> {
      if (!command.equals(starter.getCommandName())) {
        return null;
      }

      if (!starter.canProcessExternalCommandLine()) {
        return CommandLineProcessorResult.createError(IdeBundle.message("dialog.message.only.one.instance.can.be.run.at.time",
                                                                        ApplicationNamesInfo.getInstance().getProductName()));
      }

      LOG.info("Processing command with " + starter);
      int requiredModality = starter.getRequiredModality();
      if (requiredModality == ApplicationStarter.NOT_IN_EDT) {
        return new CommandLineProcessorResult(null, starter.processExternalCommandLineAsync(args, currentDirectory));
      }
      else {
        ModalityState modalityState = requiredModality == ApplicationStarter.ANY_MODALITY
                                      ? ModalityState.any() : ModalityState.defaultModalityState();
        AtomicReference<CommandLineProcessorResult> ref = new AtomicReference<>();
        ApplicationManager.getApplication().invokeAndWait(() -> {
          ref.set(new CommandLineProcessorResult(null, starter.processExternalCommandLineAsync(args, currentDirectory)));
        }, modalityState);
        return ref.get();
      }
    });
  }

  private static CommandLineProcessorResult processOpenFile(List<String> args, @Nullable String currentDirectory) {
    CommandLineProcessorResult projectAndCallback = null;
    int line = -1;
    int column = -1;
    boolean tempProject = false;
    boolean shouldWait = args.contains(OPTION_WAIT);
    boolean lightEditMode = false;

    for (int i = 0; i < args.size(); i++) {
      String arg = args.get(i);
      if (CommandLineArgs.isKnownArgument(arg) || OPTION_WAIT.equals(arg)) {
        continue;
      }

      if (arg.equals("-l") || arg.equals("--line")) {
        //noinspection AssignmentToForLoopParameter
        i++;
        if (i == args.size()) break;
        line = StringUtil.parseInt(args.get(i), -1);
        continue;
      }

      if (arg.equals("-c") || arg.equals("--column")) {
        //noinspection AssignmentToForLoopParameter
        i++;
        if (i == args.size()) break;
        column = StringUtil.parseInt(args.get(i), -1);
        continue;
      }

      if (arg.equals("--temp-project")) {
        tempProject = true;
        continue;
      }

      if (arg.equals("-e") || arg.equals("--edit")) {
        lightEditMode = true;
        continue;
      }

      if (arg.equals("-p") || arg.equals("--project")) {
        // Skip, replaced with the opposite option above
        // TODO<rv>: Remove in future versions
        continue;
      }

      if (StringUtil.isQuotedString(arg)) {
        arg = StringUtil.unquoteString(arg);
      }

      Path file = parseFilePath(arg, currentDirectory);
      if (file == null) {
        return CommandLineProcessorResult.createError(IdeBundle.message("dialog.message.invalid.path", arg));
      }

      projectAndCallback = openFileOrProject(file, line, column, tempProject, shouldWait, lightEditMode);

      if (shouldWait) {
        break;
      }

      line = column = -1;
      tempProject = false;
    }

    if (projectAndCallback != null) {
      return projectAndCallback;
    }
    else {
      if (shouldWait) {
        return new CommandLineProcessorResult(
          null,
          CliResult.error(1, IdeBundle.message("dialog.message.wait.must.be.supplied.with.file.or.project.to.wait.for"))
        );
      }

      if (lightEditMode) {
        LightEditService.getInstance().showEditorWindow();
        return new CommandLineProcessorResult(LightEditService.getInstance().getProject(), OK_FUTURE);
      }

      return new CommandLineProcessorResult(null, OK_FUTURE);
    }
  }

  private static @Nullable Path parseFilePath(String path, @Nullable String currentDirectory) {
    try {
      Path file = Path.of(FileUtilRt.toSystemDependentName(path));  // handle paths like '/file/foo\qwe'
      if (!file.isAbsolute()) {
        file = currentDirectory == null ? file.toAbsolutePath() : Path.of(currentDirectory).resolve(file);
      }
      return file.normalize();
    }
    catch (InvalidPathException e) {
      LOG.warn(e);
      return null;
    }
  }


  private static CommandLineProcessorResult openFileOrProject(Path file, int line, int column,
                                                              boolean tempProject, boolean shouldWait, boolean lightEditMode) {
    return LightEditUtil.computeWithCommandLineOptions(shouldWait, lightEditMode, () -> {
      boolean asFile = line != -1 || tempProject;
      return asFile ? doOpenFile(file, line, column, tempProject, shouldWait) : doOpenFileOrProject(file, shouldWait);
    });
  }
}