How do I synchronize AST elements and cursor position? - parsing

I am using a parser to retrieve the AST of some code typed in an editor. What I would like to achieve now is this: Given the cursor position in the editor (row, column), find the currently selected AST element. However, I have no idea how this can be done, are there any standard ways to solve this?

First you stamp each AST node with source file position (line and column number).
Second, you build a map in the editor: abstractly, for each pixel, the line and column number of the source file being displayed. (In practice, if your displayed lines are fixed height, and your displayed characters are fixed with, you can get by with a map from the displayed-line-number to the source line number).
Now mapping back and forth from screen position (e.g., cursor location) to AST node is easy, even if you edit the tree and/or change what part is displayed.
Some complications occur when you insert new tree nodes in the tree because they don't have a "file position". That's OK, you can assign them arbitrary line/column numbers that don't overlap with any existing line numbers. When you write out the modified tree to file, you don't really need the line/column numbers anymore, so you can ignore them all.

Related

Styling specific line of Text on Jetpack Compose

So, it's kind of a simple styling.
Text(
text = "some text which can extends to many lines"
)
how would someone build an annotated string to style only specif lines of the text? And by line I mean what it's actually rendering as a line - and not something predefined as a list of sub strings.
I've imagine to make a use of combined textLayoutResult with the annotated capability, but by reading the documentation I don't have much clue how to do so.
The plan was to the TextLayoutResult to retrieve the number of lines rendered on text. Then, it should be theoretically possible to retrieve the sub string on each of the lines rendered. And by pushing them on the annotated processor, the desired results would be achieved.
What am I missing?
I've managed to achieve the desired effect.
For those who want to do, you will need at least:
Two Text components;
A mutable state to keep track of offset on the text;
You limit the first Text by the number of maxLines - 1 and uses TextLayoutResult to retrieve the last offset of the last line. Then, you just update the state on the composable and create another Text component with the substring which starts from the last one. Then, you can apply the filters that you wish on this one.
Repeate the process for as many lines as you wish.

iOS How to get all words coordinates in PDF page

I have looked through many tutorials and usually stack users trow links to the pdfkitten, but as I've tested it I have not satisfied with result. So the search does not work with multiply word and etc.
So what I am looking for I need to get all words from the pdf page and highlight it if the words cross some rectangle.
I used PDFKitten for the same.
What I did was while scanning the PDF - Identify the words separated
by spaces.
Save the RenderingState(Model in PDFKitten code)word is
encountered save that word in a model with it's current
RenderingState (Model in PDFKitten code) which will be initial state.
When the complete word is found(space separated) again save the
current RenderingState as final state.
The code for converting RenderingState to actual view's frame using
above initial state and final state, is present in PDFKitten. You can
refer to that code.
apply current media box transform to frame.
And finally don't forget
to convert resulted frame into user's co-ordinate system. Otherwise
you will observe the reverse effect.

Word Openxml: how to get a text box the right size?

I'm using PHP to generate docx documents from a database. The generated document contains column charts which have labels attached (i.e. user shapes containing textboxes). In an attempt to get the textboxes to accommodate and display all of the text (i.e. it shouldn't be necessary for the user to resize a textbox to see all the text) my code calculates how many characters will fit into 3cm, adds linefeeds to the string as required and tells me how many lines of text are needed. I have:
<a:xfrm xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:off x="1638276" y="1676399"/>
<a:ext cx="1257325" cy="'.(252000 * $labelLeftLines).'"/>
</a:xfrm>
which I believe should give me a text box around 3.5cm wide (extra .5 for the internal padding) and a height of .7cm multiplied by whatever is the value of $labelLeftLines. However, the text box always turns up as 3.cm wide by .86cm high, which only ever displays one line of text.
If I add in 'autofit':
<a:bodyPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" vertOverflow="clip" wrap="square" rtlCol="0">
<a:spAutoFit/>
</a:bodyPr>
the generated file looks just the same, though, when I right click on the textbox to inspect the properties, 'autofit' is indeed applied. I have to uncheck it and recheck it to make it affect the textbox.
Any openXML gurus out there?
Hmm, some random floundering around revealed that the values I need to manipulate are here:
<cdr:relSizeAnchor xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing">
<cdr:from>
<cdr:x>0.47</cdr:x>
<cdr:y>0.75</cdr:y>
</cdr:from>
<cdr:to>
<cdr:x>0.67</cdr:x>
<cdr:y>1</cdr:y>
</cdr:to>
Changing those values does actually change the size of the texbox, though I haven't a clue what units are being used. From 0.75 to 1 produces a height of 1.43cm.
One day I'll maybe be able to find my way around the doucmentation.

Delphi RichEdit, get y-pixel start of an arbitrary line

I have a richedit containing lines using different fonts, styles, languages etc.
I am drawing in a gutter. I would like to start my drawing at the same y pixel position as the corresponding line.
Send the control an em_PosFromChar message. It returns the client coordinates of the character at the given index, although the documentation doesn't say what the coordinates represent (upper left corner, baseline center, or what). You're looking for the character's baseline.
Use em_LineIndex to get a character index for a given line number, if you don't already know the index of a character you're interested in.

Jump to specified location in dicom series (ImageJ)

I need to see specific points in a CT-scan (I've been given a mask representing a segmentation, so I know the coordinate in mm of my features of interest).
This is what I am doing at the moment in ImageJ:
File/Import/Image Sequence, I select the first dcm file in the folder of the interest, and then I import the stack
I know my point of interest is at x=10, y=5, z=20 [mm]. So I put the cursor on my image, scroll down the stack with the mouse wheel until I see z=20 in ImageJ window, then move the cursor until I match the other coordinates.
Is there a way to automatically jump to the slice corresponding to the specified location?
Yes. I would either
Write a Plugin in Java
Write a macro
I'd prefer Java because it provides more options when compared to the macro language.
In Java the code would be
imp.setSlice(20);
int poi=imp.getProcessor().getPixelValue(10,5); //int I assume but could be double
Although this is a very simple Plugin so you could also do the same in the macro language easily

Resources