Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add imports folding support #555

Closed
fbricon opened this issue Jun 8, 2018 · 16 comments
Closed

Add imports folding support #555

fbricon opened this issue Jun 8, 2018 · 16 comments
Assignees

Comments

@fbricon
Copy link
Collaborator

fbricon commented Jun 8, 2018

similar to typescript support (https://code.visualstudio.com/updates/v1_24#_import-folding)

@yaohaizh
Copy link
Collaborator

yaohaizh commented Jun 11, 2018

@aeschli For this feature, is it about https://github.com/Microsoft/vscode-languageserver-protocol-foldingprovider? And is the API still in proposed or when will be finalized into the server spec?

@aeschli
Copy link
Collaborator

aeschli commented Jun 11, 2018

Yes, that's the one. No more changes to the protocol planed from my side. I try to get it added to the 'official' APIs soon, but no need to wait for that.

@yaohaizh yaohaizh self-assigned this Jun 11, 2018
@fbricon
Copy link
Collaborator Author

fbricon commented Jun 12, 2018

Matching jdt.ls issue: eclipse-jdtls/eclipse.jdt.ls#694

@badsyntax
Copy link

Not sure if this is the right approach at all but here's a very basic example/POC of how this can be done:

const IMPORT_STATEMENT_TEST: RegExp = /^import [^;]+;$/;

class ImportFoldingRangeProvider implements FoldingRangeProvider {
	provideFoldingRanges(document: TextDocument): FoldingRange[] {
		const position = { lineStart: null, lineEnd: null };
		for (let lineNumber = 0; lineNumber < document.lineCount; lineNumber++) {
			const lineText: TextLine = document.lineAt(lineNumber);
			const isImportStatement = IMPORT_STATEMENT_TEST.test(lineText.text);
			if (isImportStatement) {
				if (position.lineStart === null) {
					position.lineStart = lineNumber;
					position.lineEnd = lineNumber;
				} else {
					position.lineEnd = lineNumber;
				}
			}
		}
		return [new FoldingRange(position.lineStart, position.lineEnd)];
	}
}

function provideImportFolding() {
	languages.registerFoldingRangeProvider({ scheme: 'file', language: 'java' }, new ImportFoldingRangeProvider());
}

Screenshot:

out

@fbricon fbricon added this to the End March 2019 milestone Mar 20, 2019
@fbricon
Copy link
Collaborator Author

fbricon commented Mar 20, 2019

With server-side folding:

Mar-20-2019 11-28-11

Now we need VS Code to allow users to define the initial collapsing state of a region. I can imagine people needing to have the imports section collapsed by default.

@fbricon
Copy link
Collaborator Author

fbricon commented Mar 20, 2019

At least, the folding state is preserved when closing/reopening the files

@fbricon fbricon closed this as completed Mar 26, 2019
@fbricon
Copy link
Collaborator Author

fbricon commented Mar 26, 2019

So a new preference is introduced:
java.foldingRange.enabled: Enable/disable smart folding range support. If disabled, it will use the default indentation-based folding range provided by VS Code.

@aeschli
Copy link
Collaborator

aeschli commented Mar 26, 2019

Note that you can achieve the same with setting in VSCode

	"[java]": {
		"editor.foldingStrategy": "indentation"
	},

@fbricon
Copy link
Collaborator Author

fbricon commented Mar 26, 2019

@aeschli indeed. The java.foldingRange.enabled preference is actually more useful as a server-side preference, which other clients can leverage.

@hgodoy
Copy link

hgodoy commented May 27, 2019

Is there a way to "fold" by default in all files?

@fbricon
Copy link
Collaborator Author

fbricon commented May 27, 2019

@hgodoy not at the moment. Please vote for microsoft/vscode#40338 (comment)

@hgodoy
Copy link

hgodoy commented May 27, 2019

@fbricon Ok. Thanks!

@justatestaccountfortdl
Copy link

justatestaccountfortdl commented Feb 1, 2023

@fbricon looks like it is broken atm. Fails even on simple things, so i had to switch back.
image
image
image

@fbricon
Copy link
Collaborator Author

fbricon commented Feb 1, 2023

@justatestaccountfortdl please define "broken". Can you share a screencast, logs?

@justatestaccountfortdl
Copy link

justatestaccountfortdl commented Feb 1, 2023

Well, defining 'broken' in the context of having to switch back to regular indentation based strategy would sound like: it does not show up any foldings whenever i turn it on. Could you clarify, what kind of logs do you need and where can i get them from?

image

image

A test sample:

// Test java class.

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;

public class Test implements A.B, C.D, E {

    private native int start(String path, int value);

    private native int write(ByteBuffer pBuffer, int iLen);

    private native void stop();
    private int bIsEnabled;

    public Boolean isBuffering() {
        if (bIsEnabled == -1) {
            return null;
        }
        return bIsEnabled == 0 ? true : false;
    }
}

Switching back to indentation based (either through strategy (replaced auto to indentation) and by turning off 'smart folding range' (which is going to affect in the same way, since it will fall back to indentation)):

image
image

https://pastie.io/rhemqo.cpp
https://pastie.io/nntuac.cpp

these logs?

@justatestaccountfortdl
Copy link

#667 i see, yet i was not using an android application project (at least on the sample screenshots). I tried to switch to pre-release mode but still.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants