When drilling down from the project dashboard, the component viewer is the ultimate place to view detailed information on a given file (or unit test file) on different axes.
Source Code File
When drilling down into measures, you will eventually reach the file level and will be able to browse the source code.
SonarQube is shipped with 5 default perspectives. Depending on the plugins that are installed, other perspectives may be available.
This perspective displays source code with highlighted syntax.
To display the new/modified source code or view the last committers, you need to install the SCM Activity Plugin.
Since version 3.6, (on Java files only), usage and declaration of variables and methods are highlighted (limited to class scope). When clicking on a variable or on a method name, it is possible to see its declaration and usage.
This perspective displays the source code decorated with information about code coverage (by unit tests and/or integration tests).
In the source code:
- The first column displays the number of unit tests covering the line (available since version 3.5, was the number of hits by unit tests in versions prior to 3.5)
- The second one displays the number of covered conditions by unit tests out of the total number of conditions to cover
When clicking on a figure displayed in the first column, the unit tests covering this specific line are listed:
When selecting Per test > Covered lines in the second dropdown list, a third dropdown list appears. In this latter list, it is possible to select a specific unit test. Then, only lines covered by this unit test are displayed:
This perspective displays issues directly in the source code.
The issues to display can be filtered by coding rule, severity, etc.
Thanks to the Time changes... list box, you can limit the display to issues that were added in the selected time span. See Differential Views for more information.
A few lines of context are provided around each issue. If necessary, you can display the full source code by ticking the Full source box.
See Duplications for more information.
Unit Test File
When drilling down into unit tests execution measures, you will eventually reach the unit test level:
This perspective displays the source code of the unit test file:
This perspective displays the result of the unit tests belonging to the unit test class. In case of a failure or an error, the reason is displayed:
It is also possible to see the mapping of unit tests and covered code. The objective is to answer the following questions:
- Which files are covered by a given unit test?
- How many lines of code are covered by a given unit test?
- Which lines are covered by a given unit test?
- Which tests cover a given line of code?
This perspective displays issues on unit tests:
The component viewer is the heart of SonarQube: it displays the source code of a file (both source and test files), and all relevant information about it:
- Overall file measures, like lines of code, complexity, number of methods, ...etc
- Issues (generated by the rules activated on the quality profile) and their related technical debt
- Duplicated blocks within the same file or in other files, from both within the project and in other projects (if cross-project duplication is activated).
- Lines and branches covered by unit or integration tests
- SCM information (if the SCM Activity Plugin is installed) like who last committed a specific line and when
- All tests of a given test file, along with their execution time and their status
You will land on the component viewer:
- when drilling down from project dashboards using hunting services.
- when reviewing issues on the Issues page or in the Issues Drilldown.
- when searching for a particular file using the search input at the top-right.
The component viewer is composed of 3 parts:
- The header lies across the top of the file. It displays useful information and offers decoration and filtering actions.
- The workspace sits along the left of the file, and is used to keep track of navigation between files.
- The source code is in the center, decorated with additional information based on the options chosen in the header.
The header can contain up to 5 tabs, one per main axis: Overall Measures, Technical Debt and Issues, Coverage (for source files) or Tests (for test files), Duplications, and SCM. Tabs which aren't relevant to the current file won't be shown. For instance, if the project has no tests, the coverage tab will be omitted. Similarly, the duplications tab will be omitted if there are no duplications, and the SCM tab will be missing if the relevant plugin is not installed.
You can click on each tab to show its detailed metrics in a row below the tabs. Click the same tab again to toggle display of the metrics row.
Each tab consists of two parts: a thin blue line at the top, which controls decoration, and the tab itself, which controls filtering.
Clicking on a tab does not have any impact on the source code that is displayed. It just shows detailed information about the code. You have to explicitly click on a filter or on a decoration bar to change something on the source code.
On top of each tab (except the first one), a light blue bar can be toggled to activate decoration of the source code with information relevant to the tab.
- The decoration action modifies which information is visible on the source code. For instance, you can click the thin blue bar at the top of the SCM tab to show the file's SCM annotations.
- Multiple decorators can be turned on at the same time.
- Once a decorator is activated (the light blue bar becomes darker) you may need to scroll down to find an instance of the relevant decoration. For instance, if you turn on the duplications decoration, the file you're looking at may not have a duplicated block at the top part of the file; you may need to scroll down to find one.
When a tab is expanded, it gives access to filtering actions:
- Filtering actions modify which part of the source code is visible.
- They can be toggled by clicking any metric in the tab that has ">" next to it. For instance, in the issues & technical debt tab, you can click on info > to show only code with info-level issues (and a few lines of context around those issues.)
This means that if you activated the "Unresolved issues" filter in the Issues tab and you expand the Coverage tab, nothing will change in the source code until you select one of the filters of the Coverage tab.
Decoration and filtering work independently of each other. For instance, it's possible to filter the source code to see only the parts where there are info issues, while keeping the coverage information displayed on those lines - like in the example below:
If you click on one of the filters available on the Issue, Coverage, or Duplication tabs, the component viewer automatically toggles the appropriate decoration for you if it's not already active.
The workspace keeps track of your navigation history when you use the features of the component viewer itself to navigate between files. It can help you:
- remember which files you've just visited
- quickly move back to one of them
Note that the workspace is automatically cleaned up once you stop navigating through these 2 features.
The source code
The main purpose of the component viewer is to show source code:
This feature is available for Java code only for the moment, and limited to the scope of the file. It allows you to answer the questions:
Eventually, this feature should be available for other languages and for identifiers declared outside the current file.
To learn more about the decorating and filtering capabilities of each of the header tabs, please see
- Overall Measures tab
- Technical Debt and Issues tab
- Coverage tab
- Tests tab
- Duplications tab
- SCM Information tab