Supply method bodies in Tools API - delphi

Years ago, I wrote a code template that took a few simple parameters (points) and produced a class skeleton. Since some of the method bodies had code in them, I couldn't call InvokeClassCompletion and just placed the implementations with the declarations (user had to move them).
It has occurred to me that I can write a code template script engine to insert these methods in the correct position, but I don't see how I'm supposed to navigate the file. There are declarations for IOTAEditReader and IOTAEditWriter in ToolsAPI.pas, but I'm guessing there is a step missing - certainly I don't need to manually parse the Delphi code just to find the implementation section?
Does anyone have experience with it?

The IDE has plenty of parsers in it, but not one is made available for IDE plugins (ToolsAPI). So you have to write your own parser or use an existing one like http://delphiblog.twodesk.com/using-the-castalia-delphi-parser

You might find that GExperts or CNPack contains almost all the code you need, in one of its editor wizards.
If not, then, those two are the best reference I know for writing IDE plugins. As far as writing parsers, see Andreas' answer.
If you just want better code templates, you could consider buying Castalia, as it has an improved code template feature over the builtin delphi IDE plugin features. Castalia internally uses the Castalia delphi parser mentioned by andreas. It's quite good.

Related

How to get type info from Go compiled packages in language x?

I want to write a simple editor with basic autocomplete functionality for the Go language as a pet project. How would one go about doing it? I took a look at the Go plugins for Eclipse and IntelliJ, but they were too big for me to comprehend (not to mention getting one to compile).
The Go standard library offers the building blocks for a Go parser which
you can use to parse the source files and look for function definitions and the like.
There's also the godoc command which
already does what you want: extracting method definitions and it's documentation. You may look in the
source code to see how godoc is
working or use godoc directly.
This editor written in Go projects has a manageable amount of code,
you may look into it.
The de facto standard approach to this problem is to use nsf's gocode. I have tried it only in Vim - it works very well.
Even though there's ready made support for specific editors, gocode is not editor specific. It's a daemon with a communication protocol. It is thus usable from any program.

any tools which scan code and give a list of functions which call a given function

here's my problem, I'm tidying up some old code which I've modified over the years and removing redundent functions,
I can do it the slow way & comment it out and see if the compiler throws an error.
But I'm just wondering if there are any tools which can which scan code and give a list of functions which call a given function.
I Had a look in ge-experts & icarus, but they only do this at the level of units & classes not down to functions.
any suggestions welcome,
many thanks, Brian
Recent versions of Delphi have "Search for References" available via context menu or Ctrl-Shift-Enter. This has the advantage over a simple "search in files" that it will find only references to the current function under the cursor and not also any function or other identifier with the same name.
Compile your project. Then, in the IDE, those lines that are accessed (rather than being dead code) will have a blue dot in the left margin:
No method is perfect, limitations of the one below are these:
the .MAP file will include functions that the linker cannot eliminate (for instance overrides of methods in classes touched by your code)
it will only give you method names, but if methods are overloaded multiple versions of these methods could be used
The big pro over using .MAP files is that they are easier to scan than blue dots in the code editor.
So it does answer your question to provide a list of functions. But that might not actually what you are after (:
Method using .MAP file:
change your project to include a detailed .MAP file
rebuild your project
the directory of your .EXE file now will include a .MAP file
scan the .MAP file for function names that are included in the .EXE
That .MAP file will exclude functions eliminated by the compiler and linker.
Those are a good indication of what 'dead' code you have.
In a similar way, you could use the JDBG information. That contains more context, but also requires you to write some tooling yourself.
For Pascal this is trivial. First make a list of all the functions, then for each function search for it in the text not following the word "function" and followed by a "(". Awk would be a good tool for doing this.
If your Delphi version is 2007 (or may be prior to it?), I strongly suggest you to consider using DGrok: Give it a try and you will see how capable it is (The demo app will tell).
As it was pointed out by the author, you still need to implement symbol table support, so that the tools can do refactorings or Find References: Roll your own...
Don't worry if you are stuck to Delphi, please head to
PasParse (The Delphi port of the DGrok parser that was originally written in C# !) by Turbo87.
Notice that Turbo87 has forked the original Joe White's Dgrok (latest release in 2008) (Update to VS2010 and add some documentation for LexScanner).

Tool that shows unit dependencies for Delphi 2010 or Delphi 7 program

We're trying to untangle a hairball of 100's of units, removing some.
It would be helpful if there was tool that would show us what units were explicitly using unit X.
Penganza doesn't seem to have a report that does that. (Although it has lots of other useful reports.)
Can anyone suggest a tool or strategy for doing this, other than just hiding unit x and then hitting F9 ... repeatedly?
MMX (Model Maker Code Explorer) has a nice unit dependency analyzer (it is especially good at detecting cycles).
For more details, see this answer.
--jeroen
From a similar question here
You might want to take a look at at
CnPack.
CnPack includes a Uses cleaner
wizard wich hasn't failed me yet.
GExperts can show Project Dependencies.
Peganza Pascal Analyzer can do the work. I haven't worked with it much, but a former dev here wrote a system that uses PAL to do the analysis, then dumped the results into a database, and then there's a browser app that lets you enter a unit name and it returns the list of units affected, whether they would need to be rebuilt if the unit changed, or if the interface changed. We use lots of BPLs so you can sometimes change a unit and you don't have to re-build other binaries that use your unit, unless the interface changed. This saves us lots of work (hundreds of BPLs and EXEs).
Chris
Headway Software's Structure 101g (and Restructure 101g) can do that really well, with the Delphi plugin.
Disclaimer: I wrote the flavors to analyze Delphi. I use them professionally, helping clients.
We've just released a freeware utility that does exactly what you need plus quite a bit more. It's called the Delphi Unit Dependency Scanner (DUDs) and you can download it here: http://www.easy-ip.net/delphi-unit-dependency-scanner.html
Sorry it's a bit late!
I was going to mention Icarus, but when I googled them I got this stack overflow answer, which you might want to check out.
Then again, sometimes I just like to delete my whole Unit Output Directory, then count my new DCU's, and that works too.
The reason you may like Icarus and not GExperts is that it doesn't rely on you to have properly maintained the uses statements in your project file.
A newcomer in this field is the Delphi Plugin for Sonar. It does not list unit dependencies but can find unused files and "dead" code (and more).
Implemented features:
Counting lines of code, statements, number of files
Counting number of classes, number of packages, methods, accessors
Counting number of public API (methods, classes and fields)
Counting comments ratio, comment lines (including blank lines)
CPD (code duplication, how many lines, block and in how many files)
Code Complexity (per method, class, file; complexity distribution
over methods, classes and files)
LCOM4 and RFC
Code colorization
Unit tests reports
Assembler syntax in grammar
Include statement
Parsing preprocessor statements
Rules
Code coverage reports
Source code highlight for unit tests
“Dead” code recognition
Unused files recognition

Generating a list of events in a Delphi/BCB Project

I would like to generate a list of events and methods assigned to them in a given BCB project. Is there a way to do this?
I don't think you can do this in a 100% reliable way. But maybe it's enough to just scan all the *.dfm's in your project folder for lines starting with " On".
There are DFM parser code available on the net.
http://www.felix-colibri.com/papers/colibri_utilities/dfm_parser/dfm_parser.html
FWIW, event is simply a property with method type.
I have just recently written a DFM file parser myself. My use case was to load a form and extract and replace some binary information from it. The library is written in Go, it is well-tested with RAD Studio source code and our own production code.
https://github.com/gonutz/dfm
You can use the parser, walk the tree in memory and generate what you need from it.

FxCop/StyleCop for Delphi?

Does anyone know of an equivalent to FxCop/StyleCop for Delphi? I would really like to get the automatic checking of style, etc. into Continuous Integration.
There's Pascal Analyzer from Peganza: http://www.peganza.com/products_pal.htm
I don't know how the features compare to FxCop, since I haven't really used either one.
The closest I've seen is CodeHealer from SOCK software. We use it, and we have integrated it into our FinalBuilder build. It differs from FxCop in one important way: It analyzes the source code, rather than the produced executable. It also doesn't check quite as much as FxCop does. But I think it is the best thing which is available in this category for Delphi.
Delphi 2009 support isn't there just yet, but they say they're working on it.
Delphi Code Analyzer is another one that is open source.
The DGrok project started with something like FxCop some years ago. The parser and analysis parts are still available, read more at "DGrok 0.8.1: multithreading, default options, GPL" - The parser is a .Net project but
DGrok is a set of tools for parsing
Delphi source code and telling you
stuff about it. Read more about it on
the DGrok project page.
There is a new Delphi plugin for Sonar, which uses a Delphi grammar to run automatic tests over the source code.
I've heard of something called Delforex but haven't used it myself (yet)
Delforex is great for actually formatting the code. It does not do much more than that though. (we have/do use it).
I would second the votes for either Pascal Analyzer or Code Healer.
Vaccano
Doesn't Delphi output .net compatible IL code? I haven't used it in an age but I thought newer versions output .net assemblies.
If so then I would have thought FXcop would work and you could always add some of your own custom rules to it. Stylecop would not work but you could at least get FXCop running.

Resources