1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +02:00

Bug 566093 - CDT LSP: null-free functions to retrieve URL from IDocument

Introduced org.eclipse.cdt.lsp.internal.text.DocumentUri implements
Function<IDocument, Optional<URI>>
Reworked usages of Server2ClientProtocolExtension#getUri(IDocument)

Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
This commit is contained in:
Alexander Fedorov 2020-08-15 11:54:52 +03:00
parent 9e303185f9
commit 46a799d96d
6 changed files with 116 additions and 13 deletions

View file

@ -10,5 +10,8 @@ Require-Bundle: com.google.gson;bundle-version="2.8.2",
org.junit,
org.eclipse.lsp4j,
org.eclipse.lsp4j.jsonrpc,
org.eclipse.text;bundle-version="3.10.0",
org.eclipse.lsp4e,
org.eclipse.cdt.lsp.core
org.eclipse.cdt.lsp.core,
org.eclipse.core.filebuffers;bundle-version="3.6.0",
org.eclipse.core.runtime;bundle-version="3.19.0"

View file

@ -0,0 +1,53 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.text.tests;
import static org.junit.Assert.assertTrue;
import org.eclipse.cdt.lsp.internal.text.DocumentUri;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.Document;
import org.junit.Test;
public class DocumentUriTest {
private final DocumentUri uri;
public DocumentUriTest() {
uri = new DocumentUri();
}
@Test
public void emptyDocument() {
assertTrue(uri.apply(new Document()).isEmpty());
}
@Test
public void externalDocument() {
Document document = new Document();
FileBuffers.createTextFileBufferManager().createEmptyDocument(new Path("some.c"), LocationKind.LOCATION);
assertTrue(uri.apply(document).isEmpty());
}
@Test
public void workspaceDocument() {
Document document = new Document();
FileBuffers.createTextFileBufferManager().createEmptyDocument(new Path("some.c"), LocationKind.LOCATION);
//it's a pity! see https://bugs.eclipse.org/bugs/show_bug.cgi?id=566044
assertTrue(uri.apply(document).isEmpty());
}
}

View file

@ -27,6 +27,7 @@ Export-Package: org.eclipse.cdt.cquery;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.internal.cquery.core;x-internal:=true,
org.eclipse.cdt.internal.cquery.ui;x-internal:=true,
org.eclipse.cdt.lsp.core;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.lsp.internal.core;x-internal:=true
org.eclipse.cdt.lsp.internal.core;x-internal:=true,
org.eclipse.cdt.lsp.internal.text;x-friends:="org.eclipse.cdt.lsp.ui"
Bundle-Activator: org.eclipse.cdt.lsp.core.Activator
Bundle-ActivationPolicy: lazy

View file

@ -24,6 +24,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -39,6 +40,7 @@ import org.eclipse.cdt.internal.ui.text.CPresentationReconciler;
import org.eclipse.cdt.internal.ui.text.PartitionDamager;
import org.eclipse.cdt.internal.ui.text.SingleTokenCScanner;
import org.eclipse.cdt.internal.ui.text.TokenStore;
import org.eclipse.cdt.lsp.internal.text.DocumentUri;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ILanguageUI;
import org.eclipse.cdt.ui.text.AbstractCScanner;
@ -72,6 +74,8 @@ import org.eclipse.ui.editors.text.TextEditor;
*/
public class PresentationReconcilerCPP extends CPresentationReconciler {
private final DocumentUri documentUri;
private CCommentScanner fSinglelineCommentScanner;
private CCommentScanner fMultilineCommentScanner;
private SingleTokenCScanner fStringScanner;
@ -126,6 +130,7 @@ public class PresentationReconcilerCPP extends CPresentationReconciler {
}
public PresentationReconcilerCPP() {
this.documentUri = new DocumentUri();
fStringScanner = new SingleTokenCScanner(getTokenStoreFactory(), ICColorConstants.C_STRING);
fMultilineCommentScanner = new CCommentScanner(getTokenStoreFactory(), ICColorConstants.C_MULTI_LINE_COMMENT);
fSinglelineCommentScanner = new CCommentScanner(getTokenStoreFactory(), ICColorConstants.C_SINGLE_LINE_COMMENT);
@ -180,9 +185,8 @@ public class PresentationReconcilerCPP extends CPresentationReconciler {
TextPresentation presentation = super.createPresentation(damage, document);
IDocument doc = textViewer.getDocument();
URI uri = Server2ClientProtocolExtension.getUri(doc);
if (uri == null) {
Optional<URI> uri = documentUri.apply(doc);
if (!uri.isPresent()) {
return presentation;
}

View file

@ -18,6 +18,7 @@ package org.eclipse.cdt.lsp.core;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.eclipse.cdt.cquery.CqueryInactiveRegions;
import org.eclipse.cdt.cquery.CquerySemanticHighlights;
@ -27,6 +28,7 @@ import org.eclipse.cdt.internal.cquery.CqueryMessages;
import org.eclipse.cdt.internal.cquery.ui.HighlightingNames;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightedPosition;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightingStyle;
import org.eclipse.cdt.lsp.internal.text.DocumentUri;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.core.resources.IFile;
@ -57,6 +59,12 @@ import org.eclipse.ui.internal.WorkbenchWindow;
@SuppressWarnings("restriction")
public class Server2ClientProtocolExtension extends LanguageClientImpl {
private final DocumentUri uri;
public Server2ClientProtocolExtension() {
this.uri = new DocumentUri();
}
@JsonNotification("$cquery/progress")
public final void indexingProgress(IndexingProgressStats stats) {
@ -84,18 +92,18 @@ public class Server2ClientProtocolExtension extends LanguageClientImpl {
public final void setInactiveRegions(CqueryInactiveRegions regions) {
URI uriReceived = regions.getUri();
List<Range> inactiveRegions = regions.getInactiveRegions();
//FIXME: AF: extract the retrieval of this document to a separate method
IDocument doc = null;
// To get the document for the received URI.
for (PresentationReconcilerCPP eachReconciler : PresentationReconcilerCPP.presentationReconcilers) {
IDocument currentReconcilerDoc = eachReconciler.getTextViewer().getDocument();
URI currentReconcilerUri = getUri(currentReconcilerDoc);
Optional<URI> currentReconcilerUri = uri.apply(currentReconcilerDoc);
if (currentReconcilerUri == null) {
if (currentReconcilerUri.isEmpty()) {
continue;
}
if (uriReceived.equals(currentReconcilerUri)) {
if (uriReceived.equals(currentReconcilerUri.get())) {
doc = currentReconcilerDoc;
break;
}
@ -138,17 +146,17 @@ public class Server2ClientProtocolExtension extends LanguageClientImpl {
URI uriReceived = highlights.getUri();
// List of PresentationReconcilerCPP objects attached with same C++ source file.
//FIXME: AF: extract the retrieval of this list to a separate method
List<PresentationReconcilerCPP> matchingReconcilers = new ArrayList<>();
for (PresentationReconcilerCPP eachReconciler : PresentationReconcilerCPP.presentationReconcilers) {
IDocument currentReconcilerDoc = eachReconciler.getTextViewer().getDocument();
URI currentReconcilerUri = getUri(currentReconcilerDoc);
if (currentReconcilerUri == null) {
Optional<URI> currentReconcilerUri = uri.apply(currentReconcilerDoc);
if (currentReconcilerUri.isEmpty()) {
continue;
}
if (uriReceived.equals(currentReconcilerUri)) {
if (uriReceived.equals(currentReconcilerUri.get())) {
matchingReconcilers.add(eachReconciler);
}
}

View file

@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.text;
import java.net.URI;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4e.LSPEclipseUtils;
@SuppressWarnings("restriction")
public class DocumentUri implements Function<IDocument, Optional<URI>> {
@Override
public Optional<URI> apply(IDocument document) {
return Optional.ofNullable(document)//
//FIXME rewrite involved static utilities and contribute the result back to LSP4E
.flatMap(d -> Optional.ofNullable(LSPEclipseUtils.getFile(d)))
.flatMap(f -> Optional.ofNullable(LSPEclipseUtils.toUri(f)));
}
}