Lexical Navigation

Yannick Gingras <[email protected]>
Newsgroups gmane.comp.kde.devel.kwrite
Message-ID <[email protected]>
In Lisp, Emacs offers a rather interesting code navigation. There are
basically only 3 short-cuts that provide most of what anyone needs:
- go to outer scope;
- go to next item with same scope;
- go to previous item with same scope.

There might be go to inner scope, but after going outer scope, it's
typically just one key stroke away to go one scope down: right arrow.

Take the following example (not any real Lisp dialect):
(def foo (a b c)
    (for (i x (enumerate (range b c))
        (ifeq (a x)
            (return i)))))
            
What the code does is not really important, but a Python version of it
would look line this:
def foo(a, b, c):
    for i, x in enumerate(range(b, c)):
        if a == x:
            return i
            
Besides being a great example of how mind twisting Lisp can be, it
illustrate beautifully how one could take advantage of lexcical
navidation. Say I just finished typing the "enumerate" clause and I
want to add a default return if we don't find any `x` that is equal to
`a`. I would go "outer scope" 3 times, "then next same scope" once. It
would put me inside that big ugly series of closing parens: ")))))",
right before the last one. I can then type Return and add my default
return clause: (return nil). That's like "return None" in Python or
"return null" in great many language.

Kate already offers something really close to this, but not the whole
solution. There is "go to matching bracket" and "previous and next
matching intent", which is just like go to next item with same scope in
Python. It's almost there! Wouldn't it be cool to be able to say: "I'm
done with this for, I just want to add something right after it"? To do
that, one would need "go to outer scope", which neither exists for
brackets nor for indents, and a small variation to "move to to next
indent" that stops at the end of the current intent rather than
entering the next block and finding the first statement that has the
same indent.

Of course, a general implementation that works for many languages is
going to be a lot more complicated than the Lisp one, which has the
benefit of only having to match parentheses, but I feel like this is a
very natural way way to move around code that might justify the
implementation complexity.

I'm also not volunteering to implement this, at least not the whole
thing. I looked at the script API, and it would be doable there, but
kind of hard because it's all character based and most of the semantic
is not passed to the script. I think that the LSP Code Actions protocol
would be the most logical way to do this since inside the LSP, there is
a lot of semantic info available. It looks pretty heavy, however, so
I'm not sure this is where a proof of concept should go. The external
tools plugin however supports everything that is needed. If I can call
a Python script, I can easily get the semeantic info back (for Python
code). I would just need a way to pass back to Kate where to move the
cursor as a response.

Let me end with two unrealated questions:
- does that sound like a way to move around code that you would like to
use?
- is there any chance that the external tools plugin would allow me to
move the cursor in a nearby future?

Sorry for the long message. I could not find a better way to summerize
my thoughts.

-- 
Yannick Gingras
http://ygingras.net
signature.asc (application/pgp-signature, 659 B)
-----BEGIN PGP SIGNATURE-----

iQGzBAABCgAdFiEEQBAbmQFumbsYmkRfiKQ8CLil3UMFAmIAvDYACgkQiKQ8CLil
3UMxzgwAgt/QSX4vwgVlNrO8gdPZW++k7DLSH+TyuNfkZjN5zayc/RmFJodpTUVn
n179ntn7KQsMff14E4Z2dXNm9kYY5ld93nvqkNqXc1CKJNmISHAkTtS2XMPaEENo
zEuROYTcQcocrl2z6/mqWlceWL0fgMbu7fYXKtN7rJ5OCxIccsxdOXIGteEDbany
OkeEXpAKRfkUPqWPzaqlUPFfCiohKjhFPDMZ/KHNHtPGoUZRFjJ8LBE+tW8w9WPE
ht55Je1rgTJet7N6nvPfrFhNuS5D1FMBRqJt+Z5DPyojeuyFd7HgkoTP0ZzUxKHv
ea+yaDZ+6/jzAHB0NaBLd98Ctg7G2OuBJ6pvOWlusFcg6R69Ta98R395zBVnaeT6
We/FHsTLFImK+Rd7KPtjVz1YWSGD+rv7lq9/I3/ITAF0/q7bwUh4YogX6UcpKdOY
qgEuuAU1ZVKFfvogaDhKWb3BM6Sor0/r3xMZ3Pj2gy5BlWl5uTdkefbNaecS0HDC
yuXLzrpm
=h1Eh
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.