KDevelop TechNotes

Issue 4: Effective code navigation

First, I'd like to explain why this issue is devoted to the code navigation. I have been looking at how KDE developers, my colleagues and students at the University work with KDevelop for quite a long time. And my experience says me that developers often do not use KDevelop all the full power when it comes to code navigation and editing. Therefore I have collected several use cases of tools available in KDevelop to edit and navigate the code. The material in this issue covers code navigation, code editing tools will be described in the next issues of KDevelop Technotes. I also assume below that IDEAl interface mode is used.

Use case 1: Switching between opened files

KDevelop has several ways to switch between files:

  1. <Alt+Left>, <Alt+Right> shortcuts to go to the tab at the left/right.

  2. File list plugin which shows a list of opened files. It also allows to select a list of files and close or reload them at once.


  3. "Switch to" function. Available from menu View | Switch To... or as a <Ctrl+/> shortcut action.
    It shows a little dialog which allows to enter a name of a file to open. It also prompts an filename autocompletion so it is not necessary to enter a complete filename.
    Note: works only for already opened files.




Use case 2: Toolview activation with keyboard

It is a common misjudge that KDevelop toolviews (like "Messages", "Application", "File Selector", "Automake Manager", etc.) can be activated only with mouse. There are actually two ways of doing the same with a keyboard.

  1. "Switch Dock" function. Functionality provided is:


Keyboard Shortcut

Menu Item

Switch Top Dock: activates the last opened toolview from the topmost dock

<Alt+Ctrl+Shift+T>

View | Tool Docks | Switch Top Dock

Switch Bottom Dock: activates the last opened toolview from the dock at the bottom (with "Messages", "Application", etc.)

<Alt+Ctrl+Shift+B>

View | Tool Docks | Switch Bottom Dock

Switch Left Dock: activates the last opened toolview from the leftmost dock (with "File Selector", "Classes", etc.)

<Alt+Ctrl+Shift+L>

View | Tool Docks | Switch Left Dock

Switch Right Dock: activates the last opened toolview from the rightmost dock (with "Automake Manager", "Documentation", "Code Snippets", etc.)

<Alt+Ctrl+Shift+T>

View | Tool Docks | Switch Right Dock



  1. <Ctlr+Alt+Left> and <Ctlr+Alt+Right> shortcuts. They switch between toolviews in the dock that was activated last in the same way as <Alt+Left> and <Alt+Right> do for the editor tabs.
    Menu items: View | Tool Docks | Previous Tool View and View | Tool Docks | Next Tool View.

  2. Dedicated shortcuts for each toolview.
    Each toolview listed in View | Tool Views menu can have it's own activation shortcut. It can be assigned in "Configure Shortcuts" dialog (menu Settings | Configure Shortcuts...) like shown in the picture below. As shown I have assigned <Alt+Ctrl+B> shortcut for the "Automake Manager". After that <Alt+Ctrl+B> can be used to activate the Automake Manager. Next time <Alt+Ctrl+B> is pressed the Automake Manager will be hidden and the editor activated.


  3. "Raise Editor" function. Use this to activate the editor at any time. Shortcut: <Alt+C>, menu item: View | Raise Editor

Use case 3: Quick opening files in the project

Sometimes it is desired to open a file contained in the current project. Usual "Open File" dialog, "File Selector" and "File Tree" plugins work but a more efficient solution exists. It's a so-called "Quick Open File" function available as File | Quick Open File menu and as <Alt+Ctrl+O> shortcut.





As you can see from the screenshots above, the dialog shows a list of files in project and provides filename filtering as you type. It is possible to type a part of the name and navigate between filtered results with up and down arrow keys.

Use case 4: Quick opening a file with class declaration and locating the starting point of a class

It is usually considered that class navigation can be done with a class view which every IDE has. Yes, of course, KDevelop has a class view but it becomes inefficient when the developer writes or examines code and suddenly decides to open certain class. The class name is known and mouse navigation should be avoided. Class view works better with a mouse so it does not fit for that task. Also it is usually hard to locate a class name in the large list of classes.

KDevelop provides the most efficient solution for this task: "Quick Open Class" function. It is available as Tools | Quick Open Class menu and <Ctrl+Alt+C> shortcut and works in the same way as Quick Open File function described above with the only difference that it works with a list of classes.





KDevelop versions up from 3.2.0 (current development version, alpha is available, release is scheduled among with KDE 3.4) have an important addition. Quick Open dialog uses a selection or a current word under the cursor to fill the class name input field like it is shown in the screenshot below. It is very useful it you want to go to the declaration of a class under the cursor.




Note: Quick Open Class works for all languages that can show classes in the class view (currently C++, Python, Ruby, Java, Perl, PHP).

Use case 5: Quick opening a file with function definition or declaration and locating the starting point of a function

This use case is the same as I described above for classes. The difference is that "Quick Open Function" (menu: Tools | Quick Open Function, shortcut: <Ctrl+Alt+M>) opens files with function definitions. If a definition is not available the declaration is opened instead.





As well as for classes, Quick Open dialog uses a selection or a current word under the cursor to fill the function name input field like it is shown in the screenshot below.




When Quick Open Function finds several functions with the name entered in function name input field, it shows additional dialog to select the desired function.




Use case 6: Monitoring current function under the cursor and navigating a list of functions in current file

KDevelop versions >= 3.1.0 have "Function Navigator" in the browser toolbar. If the cursor is positioned on function declaration or definition then fthe unction navigator shows that function. When expanded, it show a list of functions defined or declared in current file. It also jumps to the function declaration/definition for the selected function item in the combo. If a file has both function declaration and definition then function definition is preferred.

Note: function navigator can be resized with the special resize button shown after a combobox.

Note: function navigator monitoring works only in language supports which can compute the ending point of a function (currently C++).




Use case 7: C++: Switching between implementation and header file

<Shift-F12> (menu: View | Switch Header/Implementation, or editor context menu) can be used to switch between implementation and header files. File will be opened if it was not before.

Note: if you frequently use this feature you should consider to configure KDevelop to open new tabs after the current one (menu Settings | Configure KDevelop, page "User Interface", "Open new tab after current tab" setting).

Use case 8: C++: Switching between function/method definition and declaration

In case the function definition or function declaration is under the cursor, the editor context menu can be used to go to corresponding declaration/definition.
Context menu entries: "Go to Declaration of This Function", "Go to Definition of This Function".

Use case 9: C++: Navigating function definitions and declarations in current header and corresponding implementation files

A pair of C/C++ header and implementation files contains function declarations and definitions which KDevelop can easily navigate using editor context menus: "Go to Declaration" and "Go to Definition".

"Go to Declaration" shows all declarations from the header/implementation pair, "Go to Definition" shows all definitions. It does not matter whether declarations/definitions are contained in header or implementation files. Both files are searched for functions definitions and declarations.

Note: this menu can sometimes become very large. We are aware of this issue and hope to fix it in next KDevelop releases.



Discuss this article: http://www.kdevelop.org/phorum5/read.php?2,26614

© 2004 by Alexander Dymo mailto:adymo@kdevelop.org.

Permission to copy, publish and modify this document without restrictions is granted.

Everybody is welcome to share KDevelop knowledge and experience. We accept any materials for the future issues of KDevelop TechNotes.

Reviews:

23.12.2004 - first revision.