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

Get editor line height (px) and character space-width (px) by VSCode API #125341

Closed
leodevbro opened this issue Jun 2, 2021 · 17 comments
Closed
Assignees
Labels
api feature-request Request for new features or functionality

Comments

@leodevbro
Copy link

leodevbro commented Jun 2, 2021

Hi, I wrote VSCode extension, took me 6 months to build. It's a visual helper, it highlights nested code blocks. You can change block colors, depth, turn on/off focus, curly/square/round brackets, tags, python indentation and more.....

Supports: Python, PHP, JavaScript, JSX, TypeScript, TSX, C, C#, C++, Java, HTML, CSS and more...

It has already more than 1000 installs.
https://i.ibb.co/ZTkQKgR/blockman-view-extension333d.png

https://marketplace.visualstudio.com/items?itemName=leodevbro.blockman

alt text testing

The main problem is calculating or finding editor line height (px) and character space-width (px) of monospace font, I cannot find such function in the VSCode API. I need these two values to render blocks properly.

So currently the only solution is to manually adjust line height and character space-width.
For simplicity, font must be monospace, so block width will be just multiplication of char count and char space-width (px).

alt text testing

So, in order to avoid manual adjustment of these two values, please provide a function to get these two values programmatically with VSCode API.

Thanks...

@vscodebot
Copy link

vscodebot bot commented Jun 2, 2021

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@leodevbro
Copy link
Author

leodevbro commented Jun 2, 2021

Maybe it looks like a duplication of this issue:

#118994

But, I believe it needed a separate issue.

@LeoKac
Copy link

LeoKac commented Jun 3, 2021

Would love to see this feature in action!

@alexdima
Copy link
Member

alexdima commented Jun 4, 2021

Very nice! Have you tried using ch instead of px for the decorations?

@alexdima alexdima added api editor feature-request Request for new features or functionality labels Jun 4, 2021
@leodevbro
Copy link
Author

Very nice! Have you tried using ch instead of px for the decorations?

Wow, thanks @alexdima, I think ch is working, even when zooming with mouse-wheel. You are a life saver. I will update Blockman soon. And, what about line height? I think it must be even easier.

@alexdima
Copy link
Member

alexdima commented Jun 4, 2021

The line height can be straight forward computed by you. You can read the setting editor.lineHeight and then mimmic what the editor is doing internally:

/**
 * Determined from empirical observations.
 */
const GOLDEN_LINE_HEIGHT_RATIO = platform.isMacintosh ? 1.5 : 1.35;
const MINIMUM_LINE_HEIGHT = 8;

if (lineHeight === 0) { // 0 is the default
	lineHeight = Math.round(GOLDEN_LINE_HEIGHT_RATIO * fontSize); // fontSize is editor.fontSize
} else if (lineHeight < MINIMUM_LINE_HEIGHT) {
	lineHeight = MINIMUM_LINE_HEIGHT;
}

@leodevbro
Copy link
Author

Wow, seems promising. There is one problem, how can I import platform?
image

I add this line: import * as platform from 'vs/base/common/platform';
but it says: Cannot find module 'vs/base/common/platform' or its corresponding type declarations.ts(2307)

@alexdima
Copy link
Member

alexdima commented Jun 4, 2021

platform.isMacintosh is only available in our core. It is true if the OS is macOS. You can use node's process to find out the OS.

@leodevbro
Copy link
Author

Ok, and one more question. It is not crucial for now, but it would be nice to find solution. I'm getting line height correct until I zoom with mouse.

image

I can see editor.mouseWheelZoom config value which is true/false, but I could not find the current zoom level value, so maybe if zoom level is 2x, then I can just multiply lineHeight with 2.

image

There is zoom level of editor and also zoom level of window which also effects the editor zooming. So, can I always get the correct pixel value somehow? Yeah, now things are getting complicated a bit maybe.

image

@alexdima
Copy link
Member

alexdima commented Jun 4, 2021

I'm sorry, unfortunately the editor font zoom is not exposed in any way to the API.

@leodevbro
Copy link
Author

Thanks for everything. I updated Blockman already, the latest version is 1.1.1 (2021-06-05).

Also, maybe this is outside of the topic of this issue, but please check it out:

VSCode Color Decorators of styles causes wider text line, some situations are handled, some situations are not handled yet, because I managed to find some decorators with custom regex but not all.

alt text testing

Is there any way to get all the locations (line index & char index or global position) of all the color decorators? So Blockman will make certain blocks wider if necessary.

@leodevbro
Copy link
Author

Now I know the solution to get the locations (line index & char index) of all the color decorators.

const dataArr: any[] | undefined = await vscode.commands.executeCommand(
    "vscode.executeDocumentColorProvider",
    document.uri,
);

Thanks rioV8 from stackoverflow:
https://stackoverflow.com/questions/68020444/how-to-get-positions-of-all-css-color-decorators-with-vscode-api

@leodevbro
Copy link
Author

@alexdima, any ideas why Blockman not available in VS Code Web?

image

I think it's because Blockman is using node to detect OS:

image

So, maybe it is not working in web version of vscode, because web version does not have such Node support, am I right?

@aeschli
Copy link
Contributor

aeschli commented Aug 23, 2021

Documentation on how to write extensions for VS Code Web is in the works. Tracked in #130628.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

5 participants
@alexdima @aeschli @LeoKac @leodevbro and others