TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Clang-expand: Expand function invocations into current scope

58 pointsby 6keZbCECT2uBover 1 year ago

8 comments

gpderettaover 1 year ago
Very very nice. But it should really be an action in clangd instead of its own tool, for simple integration with LSP.<p>Also instead of always replacing the text, it could also be an overlay, where the function call is temporarily expanded in your IDE while in some special mode.
评论 #37588664 未加载
kevincoxover 1 year ago
rust-analyzer has this as well. I&#x27;ve never actually used it though. In most cases where I would have found it useful the function was so simple that I just did it manually without thinking. This also seems to do a better job cleaning up the resulting code than rust-analyzer does for example.<p><pre><code> let cell = self.cell_mut(pos); </code></pre> Becomes:<p><pre><code> let cell = { let ref mut this = self; &amp;mut this.board[usize::from(pos)] }; </code></pre> Instead of:<p><pre><code> let cell = &amp;mut self.board[usize::from(pos)]; </code></pre> It did manage to simplify the argument (maybe because it was the same name?) but had to rename `self`.
munroover 1 year ago
wow seriously cool idea here, i think it could be expanded on.<p>currently my only tool is &quot;jump to definition&quot; &amp; docstr tooltips, but if i could expand those definitions in place... i&#x27;d imagine i could grok the code much faster, because i could expand multiple functions and even nested functions, and read the code in one linear flow.
ahaferburgover 1 year ago
Makes me think of these two emails by John Carmack.<p><a href="http:&#x2F;&#x2F;number-none.com&#x2F;blow&#x2F;john_carmack_on_inlined_code.html" rel="nofollow noreferrer">http:&#x2F;&#x2F;number-none.com&#x2F;blow&#x2F;john_carmack_on_inlined_code.htm...</a><p>&gt; The real enemy addressed by inlining is unexpected dependency and mutation of state, which functional programming solves more directly and completely. However, if you are going to make a lot of state changes, having them all happen inline does have advantages; you should be made constantly aware of the full horror of what you are doing. When it gets to be too much to take, figure out how to factor blocks out into pure functions (and don.t let them slide back into impurity!).
CrendKingover 1 year ago
I sincerely don&#x27;t understand when is this useful. It basically removes all the existing abstractions in the code, and replaces with inline code. Those abstractions were created by someone for some reasons. Blindly destroying them in the name of &quot;refactoring&quot; is just wrong. Refactoring should be process that involves intellectual creativity, involves deeply understanding the intricacy of dependencies and references, not mechanically expanding functions and call it done.<p>Of course, just like rust-analyzer&#x27;s &quot;Expand macro recursively&quot; command, this could be used as an analysis utility, even better if it works not only on functions but also C&#x2F;C++ macros.
评论 #37596406 未加载
评论 #37595191 未加载
MauranKilomover 1 year ago
Maybe I&#x27;m the only one, but... why?<p>What about by-hand-inlining `std::find` makes the code better?
评论 #37593167 未加载
评论 #37591666 未加载
评论 #37594705 未加载
评论 #37594665 未加载
TeMPOraLover 1 year ago
Thank you! It&#x27;s a tool I dreamed of having for years, but couldn&#x27;t arse myself to try and write.<p>The output seems nice enough it should be possible to integrate it with Emacs to show as overlay, vs. actually replacing code - though in practice, I&#x27;d probably want a mix of both.
smasher164over 1 year ago
This is so cool! I want this for every language now.