KDevelop4/Manual/Arbejdet med kildekode

From KDE UserBase Wiki
Revision as of 10:09, 21 May 2011 by Claus chr (talk | contribs) (Importing a new version from external source)

Arbejdet med kildekode

Ved siden af fejlsøgning er skrivning og læsning af kildekode det, som du vil bruge mest tid med, når du udvikler software. Til dette formål giver KDevelop dig mange forskelige måder at udforske kildekoden og effektivisere skrivning af den. Som det vises i detaljer i de følgende afsnit, så er KDevelop ikke blot en kodeeditor — den er et system til at håndtere kildekode, som giver dig forskellige visninger af information, som findes i de filer, som tilsammen udgør kildekoden i din session.

Værktøjer og visninger

Til arbejdet med projekter har KDevelop begrebet værktøjer. Et værktøj giver dig en særlig visning af kildekoden eller en handling, som det kan udføre. Værktøjer repræsenteres af knapper, som findes rundt omkring i vinduets kant (med lodret tekst langs venstre og højre kant eller vandret langs bunden). Hvis du klikker på dem, så udvides de til et undervindue — en visning — indenfor hovedvinduet. Hvis du klikker på værktøjsknappen en gang til, så forsvinder visningen igen.

Note

For at få et undervindue til af forsvinde kan du også klikke på knappen x øverst til højre i undervinduet; dette fjerner dog også den tilhørende værktøjsknap fra vindueskanten, i hvert fald i KDevelop 4.2.x; og den skal du bruge for at få undervinduet frem igen. Se herunder, hvordan du får knappen tilbage til vindueskanten på samme måde, som du får enhver anden værktøjsknap placeret der. Denne opførsel er rapporteret som bug 270018)

Billedet ovenfor viser et eksempel på værktøjer placeret i den venstre og højre kant. I billeder er værktøjet Klasser åbent til venstre og værktøjet Tekststumper til højre sammen med en editor til en kildefil i midten. I praksis vil du nok kun have editoren åben og måske værktøjet Klasser eller Kodebrowser til venstre det meste af tiden. Andre værktøjer vil sikkert kun være åbne midlertidigt mens du bruger dem, sådan at der bliver mere plads til editoren.

Når du kører KDevelop første gang, så skulle du allerede have værktøjsknappen Projekter. Klik på den: det vil åbne et undervindue, som forneden viser de projekter, som du har føjet til sessionen og en visning af mapperne i dit projekt for oven.

Der er mange andre værktøjer, som du kan bruge i KDevelop, men deres knapper er ikke alle til stede i vindueskanten fra begyndelsen. For at føje flere til vælger du menuen Vindue -> Tilføj værktøjsvivning.... Her er nogle, som du sikkert vil få glæde af:

  • Classes: A complete list of all classes that are defined in one of the projects or your session with all of their member functions and variables. Clicking on any of the members opens a source editor window at the location of the item you clicked on.
  • Documents: Lists some of the more recently visited files, by kind (e.g. source files, patch files, plain text documents).
  • Code Browser: Depending on your cursor position in a file, this tool shows things that are related. For example, if you are on an #include line, it shows information about the file you are including such as what classes are declared in that file; if you are on an empty line at file scope, it shows the classes and functions declared and defined in the current file (all as links: clicking on them brings you to the point in the file where the declaration or definition actually is); if you are in a function definition, it shows where the declaration is and offers a list of places where the function is used.
  • File system: Shows you a tree view of the file system.
  • Documentation: Allows you to search for man pages and other help documents.
  • Snippets: This provides sequences of text that one uses over an over and doesn't want to write every time. For example, in the project from which the picture above was created, there is a frequent need to write code like for (typename Triangulation<dim>::active_cell_iterator cell = triangulation.begin_active(); cell != triangulation.end(); ++cell). This is an awkward expression but it will look almost exactly like this everytime you need such a loop — which would make it a good candidate for a snippet.
  • Konsole: Opens a command line window inside KDevelop's main window, for the occasional command you may want to enter (e.g. to run ./configure).

A complete list of tools and views is given here.

For many programmers, vertical screen space is the most important. To this end, you can arrange your tool views at the left and right margin of the window: to move a tool, click on its symbol with the right mouse button and select a new position for it.

Modes and working sets

If you've gotten this far, take a look at the upper right of the KDevelop main window: As shown in the picture, you will see that there are three modes KDevelop can be in: Code (the mode we discuss in the current chapter on working with source code), Debug (see Debugging programs) and Review (see Working with version control systems).

Each mode has its own set of tools that are stacked around the perimeter, and each mode also has a working set of currently open files and documents. Furthermore, each such working set is associated with a current session, i.e. we have the relationship shown at the right. Note that the files in the working set come from the same session, but they may come from different projects that are part of the same session.

If you open KDevelop the first time, the working set is empty — there are no open files. But as you open files for editing (or debugging, or reviewing in the other modes) your working set grows. The fact that your working set is non-empty is indicated by a symbol in the tab, as shown on the right. You will notice that whenever you close KDevelop and later start it again, the working set is saved and restored, i.e. you get the same set of open files.

If you hover your mouse over the symbol for the working set, you get a tooltip that shows you which files are currently open in this working set (here: the step-32.cc and step-1.cc files). Clicking on the red minus sign closes the tab for the corresponding file. Maybe more importantly, clicking on the correspondingly named button allows you to close the entire working set at once (i.e. to close all currently open files). The point about closing a working set, however, is that it doesn't just close all files, it actually saves the working set and opens a new, still empty one. You can see this here:

Note the two symbols to the left of the three mode tabs (the heart and the unidentifiable symbol to its left). Each of these two symbols represents a saved working set, in addition to the currently open working set. If you hover your mouse over the heart symbol, you'll get something like this:

It shows you that the corresponding working set contains two files and their corresponding project names: Makefile and changes.h. Clicking Load will close and save the current working set (which as shown here has the files tria.h and tria.cc open) and instead open the selected working set. You can also permanently delete a working set, which removes it from the set of saved working sets.

Some useful keyboard shortcuts

KDevelop's editor follows the standard keyboard shortcuts for all usual editing operations. However, it also supports a number of more advanced operations when editing source code, some of which are bound to particular key combinations. The following are frequently particularly helpful:

Jumping around in code
Ctrl+Alt+O Quick open file: enter part of a filename and select among all the files in the current session's projects' directory trees that match the string; the file will then be opened
Ctrl+Alt+C Quick open class: enter part of a class name and select among all class names that match; the cursor will then jump to the class declaration
Ctrl+Alt+M Quick open function: enter part of a (member) function name and select among all names that match; note that the list shows both declarations and definitions and the cursor will then jump to the selected item
Ctrl+Alt+Q Universal quick open: type anything (file name, class name, function name) and get a list of anything that matches to select from
Ctrl+Alt+N Outline: Provide a list of all things that are happening in this file, e.g. class declarations and function definitions
Ctrl+, Jump to definition of a function if the cursor is currently on a function declaration
Ctrl+. Jump to declaration of a function or variable if the cursor is currently in a function definition
Ctrl+Alt+PageDown Jump to next function
Ctrl+Alt+PageUp Jump to previous function
Ctrl+G Goto line
Searching and replacing
Ctrl+F Find
F3 Find next
Ctrl+R Replace
Ctrl+Alt+F Find-Replace in multiple files
Other things
Ctrl+_ Collapse one level: remove this block from view, for example if you want to focus on the bigger picture within a function
Ctrl++ Expand one level: undo the collapsing
Ctrl+D Comment out selected text or current line
Ctrl+Shift+D Comment in selected text or current line
Alt+Shift+D Document the current function. If the cursor is on a function or class declaration then hitting this key will create a doxygen-style comment pre-populated with a listing of all parameters, return values, etc.
Ctrl+T Transpose the current and the previous character
Ctrl+K Delete the current line (note: this is not just emacs' "delete from here to the end of the line")