lineWrapping count


#1

Hello CodeMirror community

Is there a way to get the count of a wrapped line.
Like to get the number 5 if the line is wrapped five times.

I have one solution. To divide the line.height of a wrapped line with the line.height of not wrapped lines.
A not wrapped line has a line.height of 24 and a five time wrapped line has a line.height of 120
120 / 24 = Wrapped 5 times.

But this is not the smartest way i think because sometimes the response of the line.height is to fast so that im getting a wrong height.

Here is the place where im logging the line

cmPlayground.on('renderLine', function(instance, line, element) {
    console.log(line)
    console.log(line.height)
})

Thanks

Ali


#2

Definitely don’t use renderLine for this. A relatively solid way to do do this is probably to get the top and bottom of a line using heightAtLine, and then repeatedly call coordsChar along the left of the line (or the right in right-to-left text) to identify the separate lines — i.e. first call it a few pixels below the top, see what the bottom of that box is, go a bit below that and look for the next box, etc. This is still going to be pretty awkward, though. Why do you need it?


#3

hey @marijn thx for your help. i have to try your solution.

What I’m trying is this:

I have two CodeMirror text areas. The one on the left side is to add a calculation (calc), the one on the right side is showing the result of the calc lines. As you can see in the image above, the place of the result doesn’t match to the calc lines on the left side. The problem is the wrapped line of the second (big) calc. The result line needs the same amount of wrapped lines like the calculation line.

So my idea was to get the count of the wrapped lines of the calculation, an add the same to the result side, to push the result down to the bottom to get this result here:


#4

Ah, in that case, maybe don’t count lines, but just count pixels (which is easy with heightAtLine) and insert line widgets to fill up the extra space (if any). The merge addon does something like this when you enable the align option.