8 ms·
Stack Graphs
- parhamn 5y agoAre there any editors that let you edit code in a structure closer to the way the language handles it (e.g. graphs/stacks)? You could achieve a lot more than the conventional file-per-module approach. Off the top of my head benefits could include: much easier refactoring, comments bound to specific tokens, function level versioning, much smarter git diffs, and so much more. The mapping graphs to flat-files thing feels especially silly when I do FE dev. Manipulating JSX doms on top of the JS function stack is a constant reminder of how the flattening step feels unnecessary.
- hiaux0 5y agoThe Dion editor [1] is trying to tackling this problem, if I understand correctly. [2] has some sweet GIFs for you. [1]: https://dion.systems/dion_format.html https://dion.systems/dion_format.html [2]: https://dion.systems/gallery.html https://dion.systems/gallery.html
- sweetsocks21 5y agoI don't know of any generic editor that can take a current (popular) language and do that. There is however a lot going on in this space, and I'll list some examples. Most are about editing the tree structure better, but some do branch out a bit more to the graph idea. https://docs.darklang.com/structured-editing https://docs.darklang.com/structured-editing https://hazel.org/ https://hazel.org/ http://lighttable.com/ http://lighttable.com/ Emacs has some structural editing plugins like ParEdit Many graph/node based editors like Blender shaders (3D), Reaktor (music) https://www.unisonweb.org/ https://www.unisonweb.org/ (this has some of the function versioning ideas) https://enso.org/ https://enso.org/ Smalltalk There's many many more, these are just some I remembered off the top of my head.
- parhamn 5y agoI appreciate the links!
- ball_of_lint 5y agoLisp + SLIME is probably the closest thing that exists to what you're describing.
- dcreager 5y agoOP author here. I also gave a talk about this at Strange Loop back in October if folks want to watch/listen instead of read: https://dcreager.net/talks/2021-strange-loop/ https://dcreager.net/talks/2021-strange-loop/
- billconan 5y agoThank you very much for this and for open sourcing it! For production, is there a good database system that can index this graph structure? for incremental update, how do you prune deprecated part of the graph (for example, removed/renamed files/functions?) and for this example (function_definition name: (identifier) @name) @function { node @function.def attr (@function.def) kind = "definition" attr (@function.def) symbol = @name edge @function.containing_scope -> @function.def } how can it guarantee the python shadowing rule? it doesn't seem to encode any order preference. does the code traverse the source file in the reverse order basically? And, probably not closely related to stack graph, but about using tree-sitter for c/c++ understanding, how to handle the preprocessor? because the c preprocessor can make the code look like a completely different language and mess up the parser. And how to prune and simplify CST to AST at scale (supporting many languages)?
- dcreager 5y ago> For production, is there a good database system that can index this graph structure? For awhile, we were storing this in a (very large) MySQL database, sharded with Vitess. The sharding behavior worked great (since repo ID gives you a nice sharding key), but we found that it wasn't elastic enough for our needs, since we quickly filled up the available capacity of the machines that we had reserved. Since then we've switched over to storing this data in Azure Blob Storage, basically using it as a glorified key/value store. We had to write custom logic for deciding how to structure our data so that we can efficiently write it at index time and read it at query time, but so far it's been working quite nicely! > for incremental update, how do you prune deprecated part of the graph Short version is that we're storing everything on a per-file basis. So whenever a file is changed, we generate a new stack graph snippet for that file. There might be lots of content in that stack graph that is identical to the stack graph of the previous version of the file, but we don't try to do any structural sharing more fine-grained than the file. Right now we aren't going in any pruning old files that aren't being touched by any active queries, but we could. Or move it to a colder storage tier in Blob Storage, something like that. At least for now, the marginal costs of storing the data for longer aren't our cost bottleneck.
- jandinter 5y agoLooks neat! I wish this was available for legal texts, making it easy to jump from one law to the referenced next legal provision. Many legal provisions, especially in very regulated areas, make use of “functions” “imported” from other, totally different laws. Sorry for being off-topic, but if anyone knows a resource for that, I am super interested!
- ova-throwaway 5y ago? baby lawyer and former dev here: don't we have that anyways? E.g. on Casetext, Lexis, all the usual legal research sites. I personally haven't encountered a situation where it was totally lacking.
- jandinter 5y agoMany available options seem to be based on manual annotation and, therefore, cover a limited range of all legal texts. Especially with regard to regulatory topics, those research sites usually fall short.
- ss108 5y agoI would be interested to know where you're encountering these issues, specifically. I'm interested in legal tech, would like to know where the gaps are
- masklinn 5y ago> Many available options seem to be based on manual annotation I’m not sure there’s an alternative: if a reference to an other text is complete (and thus fully disambiguated) it’s reasonably easy to infer it, but if it’s only partial and thus ambiguous (e.g. Article 54) then it becomes a lot more problematic: what happens legally if the system misinterprets the reference (e.g. to the current law’s article 54 but nearby contextual clues made it clear that it was some other text’s) and the reader follows this misinterpretation?
- masklinn 5y ago> I wish this was available for legal texts, making it easy to jump from one law to the referenced next legal provision. Many legal provisions, especially in very regulated areas, make use of “functions” “imported” from other, totally different laws. I mean, it "is", to the extent that if you put in the work of hyperlinking all the things during the digitizing process they can be. Légifrance is fairly highly (though nowhere near completely) hyperlinked for instance, here's one of the laws I selected from the front page: https://www.legifrance.gouv.fr/jorf/id/JORFTEXT000044446848 https://www.legifrance.gouv.fr/jorf/id/JORFTEXT000044446848 Many (though nowhere near all) the legal texts being referenced, modified, or inserted (as references, into other texts) are hyperlinked.
- armchairhacker 5y agois this from Github semantic (https://github.com/github/semantic https://github.com/github/semantic)? Seems very suspicious since it’s the same goal using the same technologies. The latest commit is 4mo ago but i assume they have a closed-source version they’ve been working on.
- dcreager 5y agoIt's from the same team (which I am the manager of), but it's not using that same codebase. In Semantic, you would have to write Haskell code to add support for a new language, and we've found that the declarative DSLs that tree-sitter provides are a lower barrier to entry. (Semantic also uses tree-sitter for the parsing step, btw.) We do still have plans for Semantic, but our stack graph code does not live there. [Edit] Also, the stack graph implementation is also open-source, just like Semantic, and we do our development on the core algorithms directly there. The Python extraction rules have not yet been moved over to the public tree-sitter-python language repo, but that's on the docket. Future language support would happen directly in each language's public open-source tree-sitter repo. https://github.com/github/stack-graphs/ https://github.com/github/stack-graphs/
- deleted 5y ago[deleted]
- EgoIncarnate 5y agoAre the extraction rules in the stack-graphs repository or other public repository, or in a private repository? It would aid understanding to have a fuller working example.
- dcreager 5y agoThey're currently in a private repo, only because they use an old crusty implementation of the graph DSL. We're porting them over to the open-source graph DSL (tree-sitter/tree-sitter-graph), and we'll add them to the tree-sitter-python repo as part of that effort. As mentioned above, future language development will happen directly in the per-language tree-sitter repos using the open-source graph DSL implementation.
- cloogshicer 5y agoI just want to say: This is an absolutely fantastic blog post. So well written, and really well done graphics that help understanding.
- dcreager 5y agoThanks very much, I appreciate the kind words!
- spinningslate 5y agoCool to see this is inspired by Eelco Visser's [1] group at TU-Delft, who originated the concept of scope graphs [2]. The back story of their language design research and tools is worth reading [3]. Not least because Nix originated as their build system! [1] https://eelcovisser.org/ https://eelcovisser.org/ [2] https://github.blog/2021-12-09-introducing-stack-graphs/ https://github.blog/2021-12-09-introducing-stack-graphs/ [3] https://eelcovisser.org/blog/2021/02/08/spoofax-mip/ https://eelcovisser.org/blog/2021/02/08/spoofax-mip/ -- EDIT: Fixed formatting.
- dcreager 5y agoHard agree, this is very much a “standing on the shoulders of giants” situation. Eelco's group has done amazing work on scope graphs, and we would not have been able to make this without that work as a foundation.
- spinningslate 5y ago...and I should have said: great that you acknowledged it in your blog too.
- gorgoiler 5y agoHow do I plumb this precise code jumping into vim? My tags files just don’t seem to cut it any more.
- bbkane 5y agoNeoVim has Tree-sitter and Language Server integration. I couldn't make it work for me, bit lots of people really love it!
- botdan 5y agoIf you haven't tried again recently the neovim team has done a ton of work updating the documentation on nvim-lspconfig [1]. There's also projects like kickstart.nvim [2] which aim to provide a very simple starting point for new users. It's "batteries-included" neovim which notably includes LSP, TreeSitter, completion engines, and some basic git functionality. [1] https://github.com/neovim/nvim-lspconfig https://github.com/neovim/nvim-lspconfig [2] https://github.com/nvim-lua/kickstart.nvim https://github.com/nvim-lua/kickstart.nvim
- dcreager 5y agoThe most straightforward option would probably be to write an LSP wrapper around the stack graphs code. One of the people on my team wrote a very “contrib directory” version of that for internal testing, which lets us test our stack graph rules for a new language in something like VS Code before deploying to production. If someone were to write a more polished version of that, that would be a great way to get code nav into any LSP-compatible editor for any language that supports stack graphs.
- Annili 5y ago> One of the people on my team wrote a very “contrib directory” version of that for internal testing If that is open-source, could you please share a link to it? I'm curious what advantages this "stack-graphs LSP wrapper" has over existing language-specific LSP servers for an editor/IDE, maybe lower memory usage or better performance?
- 5y ago
- ChrisMarshallNY 5y agoThis looks like something that's been available in Xcode (Swift) for a long time. I use it constantly. Makes navigating large codebases fairly straightforward. I'm glad to see it being made available to other languages. I don't know that much about Python, but I think that it's sort of a "JIT compiled" language, so this capability is pretty damn impressive. Swift is compiled and has the LLVM Toolchain system. A lot of the symbol resolution and instrumentation depends on that. I remember when MS brought GitHub, everyone was declaring that this was the end of everything, but I've been seeing GH do some really cool stuff, since. A lot of it isn't stuff that I can use, but I can still appreciate it.
- _ZeD_ 5y agoyeah, it's also available in eclipse, idea and whatever other IDE under the sun. but let's reinvent the weel again :)
- dcreager 5y agoI talk a bit in my FOSDEM talk from last year how the “local editor” and “hosted service” versions of code navigation have enough real differences that it's not obviously best to reuse local editor solutions in a hosted service like GitHub: https://dcreager.net/talks/2020-fosdem/ https://dcreager.net/talks/2020-fosdem/
- dcreager 5y agoYou touch on an important difference between languages. A lot of existing tooling in this space focuses on static languages, exactly because there are existing tools that you can build on to get something implemented more quickly. But, very few of those existing tools work for multiple static languages. LSP does, in that it provides a standard data API that all of the language-specific tools can implement — but you still have to implement the concepts once for each language that you want to support. There are relatively fewer existing tools in this space for dynamic languages. (Sorbet etc for Ruby are good examples of ones that do exist.) The ones that do require a fair bit of effort to implement, because you can't piggy-back on existing bits of a compiler, since there isn't one! (The analogous parts of a runtime interpreter tend to be much harder to piggy-back on for analysis purposes, since they all have a deeply ingrained assumption that you're in the middle of trying to execute the code.) So, the end result is that the “delta” between existing tools and what stack graphs provide is a bit bigger for a language like Python than (for instance) Go or Java. And that's a roundabout description of one of the reasons we had for targeting Python first!
- xvilka 5y agoMeanwhile their Tree-Sitter-based semantic parser[1] looks abandoned. There is even rotting for years pull request[2] adding support of the same stack graphs into it. [1] https://github.com/github/semantic https://github.com/github/semantic [2] https://github.com/github/semantic/pull/535 https://github.com/github/semantic/pull/535
- onionisafruit 5y agoThey’re no longer interested in anything from the pre-microsoft days.
- dcreager 5y agoAs mentioned elsewhere on this thread, stack graphs and Semantic were built by the same team (which I manage). Semantic is not abandoned, we've just been focusing on a different layer of our tech stack for the past year or so. https://news.ycombinator.com/item?id=29501389 https://news.ycombinator.com/item?id=29501389 That PR on the Semantic repo was our first attempt at implementing these ideas. We decided to reimplement it in a separate library (also open source, https://github.com/github/stack-graphs https://github.com/github/stack-graphs), which only builds on tree-sitter directly so that there's an easier story for us and language communities to add support for new languages. It's a fair point that we could have closed the Semantic PR to indicate that more clearly.
- alew1 5y agoVery cool! The StrangeLoop talk includes an example where you infer that Stove() returns a Stove object. If someone writes something like `f(x).broil()`, do you need to do some kind of type inference to figure out what class f(x) is? What cases do Stack Graphs fail to handle? (e.g., I assume dynamic modification of .__dict__ can't be tracked; are there other representative examples?)
- dcreager 5y agoYour `f(x)` example is similar to one of the harder examples I mention (but don't dive into) at the end of the blog post. You need a way to pass along information about `x` (such as its type) so that whatever you've constructed to model the body of `f` can use that information, should it need to. We have a notion of “scope stacks”, which live along side the symbol stacks, which we use to encode this kind of information. This early design doc goes into more detail about how scope stacks work, including a worked example that uses them: https://github.github.com/stack-graph-docs/ https://github.github.com/stack-graph-docs/ Dynamic modification of `__dict__` is definitely something that would be hard or impossible to track, depending on what kind of modification you're doing. If the keys are all string literals, then you could probably still get something approximate that's still useful. It's when the keys are constructed from arbitrary computation that it gets impossible, at least with the current stack graph framework. You'd have to have some way to lift that computation into the graph structure, so that the path-finding algorithm could simulate its execution. All while maintaining the zero-config and incremental requirements. https://twitter.com/dcreager/status/1467654252516589571 https://twitter.com/dcreager/status/1467654252516589571
- a-dub 5y agohow does it compare to doxygen's call graphs?
- franky47 5y agoNice post, though the title evoked another idea: visually representing the tech stack used in repositories. But maybe we don't need a glorification of complex stacks.