Skip to content

Commit

Permalink
Create file on hyperlink
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr authored and mickaelistria committed Nov 9, 2022
1 parent 442878c commit 387b4ab
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 23 deletions.
83 changes: 63 additions & 20 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
*******************************************************************************/
package org.eclipse.lsp4e;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -64,6 +66,7 @@
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextOperationTarget;
Expand All @@ -74,6 +77,7 @@
import org.eclipse.lsp4e.refactoring.CreateFileChange;
import org.eclipse.lsp4e.refactoring.DeleteExternalFile;
import org.eclipse.lsp4e.refactoring.LSPTextChange;
import org.eclipse.lsp4e.ui.Messages;
import org.eclipse.lsp4e.ui.UI;
import org.eclipse.lsp4j.Color;
import org.eclipse.lsp4j.CompletionParams;
Expand Down Expand Up @@ -558,14 +562,18 @@ public static void open(String uri, Range optionalRange) {
}

public static void open(String uri, IWorkbenchPage page, Range optionalRange) {
open(uri, page, optionalRange, false);
}

public static void open(String uri, IWorkbenchPage page, Range optionalRange, boolean createFile) {
if (uri.startsWith(HTTP)) {
if (uri.startsWith(INTRO_URL)) {
openIntroURL(uri);
} else {
openHttpLocationInBrowser(uri, page);
}
} else {
openFileLocationInEditor(uri, page, optionalRange);
openFileLocationInEditor(uri, page, optionalRange, createFile);
}
}

Expand Down Expand Up @@ -606,45 +614,80 @@ protected static void openHttpLocationInBrowser(final String uri, IWorkbenchPage
});
}

protected static void openFileLocationInEditor(String uri, IWorkbenchPage page, Range optionalRange) {
protected static void openFileLocationInEditor(String uri, IWorkbenchPage page, Range optionalRange,
boolean createFile) {
// Open file uri in an editor
IEditorPart part = null;
IDocument targetDocument = null;
IResource targetResource = findResourceFor(uri);
try {
if (targetResource != null && targetResource.getType() == IResource.FILE) {
if (!targetResource.exists() && createFile) {
// The file to open is not found, open a confirm dialog to ask if the file must
// be created.
if (MessageDialog.openQuestion(UI.getActiveShell(), Messages.CreateFile_confirm_title,
Messages.bind(Messages.CreateFile_confirm_message, uri))) {
try (final ByteArrayInputStream input = new ByteArrayInputStream("".getBytes())) //$NON-NLS-1$
{
((IFile) targetResource).create(input, IResource.KEEP_HISTORY, null);
} catch (Exception e) {
LanguageServerPlugin.logError(e);
}
} else {
return;
}
}
part = IDE.openEditor(page, (IFile) targetResource);
} else {
URI fileUri = URI.create(uri).normalize();
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileUri);
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileUri);
IFileInfo fetchInfo = fileStore.fetchInfo();
if (!fetchInfo.isDirectory() && fetchInfo.exists()) {
if (!fetchInfo.isDirectory()) {
if (!fetchInfo.exists() && createFile) {
// The file to open is not found, open a confirm dialog to ask if the file must
// be created.
if (MessageDialog.openQuestion(UI.getActiveShell(), Messages.CreateFile_confirm_title,
Messages.bind(Messages.CreateFile_confirm_message, uri))) {
try {
fileStore.getParent().mkdir(EFS.NONE, null);
try (final OutputStream out = fileStore.openOutputStream(EFS.NONE, null)) {
out.write("".getBytes()); //$NON-NLS-1$
}
} catch (Exception e) {
LanguageServerPlugin.logError(e);
}
} else {
return;
}
}

part = IDE.openEditorOnFileStore(page, fileStore);
}
}

} catch (PartInitException e) {
LanguageServerPlugin.logError(e);
}

// Update selection (if needed) from the given range
if (optionalRange != null && part != null && part.getEditorSite() != null
&& part.getEditorSite().getSelectionProvider() != null) {
ITextEditor textEditor = Adapters.adapt(part, ITextEditor.class);
if (textEditor != null) {
targetDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
}

} catch (PartInitException e) {
LanguageServerPlugin.logError(e);
}
try {
if (targetDocument != null
&& part != null && part.getEditorSite() != null && part.getEditorSite().getSelectionProvider() != null
&& optionalRange != null)
{
ISelectionProvider selectionProvider = part.getEditorSite().getSelectionProvider();

int offset = toOffset(optionalRange.getStart(), targetDocument);
int endOffset = toOffset(optionalRange.getEnd(), targetDocument);
selectionProvider.setSelection(new TextSelection(offset, endOffset > offset ? endOffset - offset : 0));
try {
if (targetDocument != null) {
ISelectionProvider selectionProvider = part.getEditorSite().getSelectionProvider();
int offset = toOffset(optionalRange.getStart(), targetDocument);
int endOffset = toOffset(optionalRange.getEnd(), targetDocument);
selectionProvider
.setSelection(new TextSelection(offset, endOffset > offset ? endOffset - offset : 0));
}
} catch (BadLocationException e) {
LanguageServerPlugin.logError(e);
}

} catch (BadLocationException e) {
LanguageServerPlugin.logError(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageServerPlugin;
import org.eclipse.lsp4e.LanguageServiceAccessor;
import org.eclipse.lsp4e.ui.UI;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentLinkParams;
import org.eclipse.lsp4j.TextDocumentIdentifier;
Expand Down Expand Up @@ -63,22 +64,25 @@ public String getHyperlinkText() {

@Override
public void open() {
LSPEclipseUtils.open(uri, null);
LSPEclipseUtils.open(uri, UI.getActivePage(), null, true);
}

}

@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
final IDocument document = textViewer.getDocument();
if (document == null) {
return null;
}
URI uri = LSPEclipseUtils.toUri(document);
if (uri == null) {
return null;
}
final DocumentLinkParams params = new DocumentLinkParams(new TextDocumentIdentifier(uri.toString()));
try {
return LanguageServiceAccessor
.getLanguageServers(textViewer.getDocument(),
.getLanguageServers(document,
capabilities -> capabilities.getDocumentLinkProvider() != null)
.thenApplyAsync(languageServers -> {
IHyperlink[] res = languageServers.stream()
Expand Down
2 changes: 2 additions & 0 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class Messages extends NLS {
public static String DocumentContentSynchronizer_OnSaveActionTimeout;
public static String DocumentContentSynchronizer_TimeoutMessage;
public static String DocumentContentSynchronizer_TimeoutThresholdMessage;
public static String CreateFile_confirm_title;
public static String CreateFile_confirm_message;

static {
NLS.initializeMessages("org.eclipse.lsp4e.ui.messages", Messages.class); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ symbolsInFile=Symbols in {0}

DocumentContentSynchronizer_OnSaveActionTimeout="On Save Action Timeout"
DocumentContentSynchronizer_TimeoutMessage="On-Save action timeout out after {0} seconds for {1}"
DocumentContentSynchronizer_TimeoutThresholdMessage="On-Save action timeout out after {0} seconds for {1}. On-Save actions will no longer be called for this document"
DocumentContentSynchronizer_TimeoutThresholdMessage="On-Save action timeout out after {0} seconds for {1}. On-Save actions will no longer be called for this document"

CreateFile_confirm_title=Create file
CreateFile_confirm_message=Unable to open file ''{0}''. Do you want to create file?

0 comments on commit 387b4ab

Please sign in to comment.