目录

Editor Basics

DevEco Studio allows you to develop apps/atomic services in multiple programming languages, including ArkTS, JS, and C/C++. When writing code, make most of the following editor tips to improve your work productivity.

Syntax/Semantic Highlighting

The editor displays code in different colors by keyword, operator, string, class, identifier, comment, and more. To customize the color scheme, choose File > Settings on the menu bar (on macOS, choose DevEco Studio > Preferences/Settings) and then choose Editor > Color Scheme from the left pane. The color scheme configured under Language Defaults applies to projects of all programming languages. To set the color scheme specific to a programming language, select the language from the left pane, and deselect Inherit values from on the right pane.

Code Navigation

You can move about your code with ease. To jump to the definition of a symbol, such as a class, method, parameter, or variable, just click it while pressing and holding Ctrl (Command on macOS). To find references to a symbol, simply click the symbol. If there is only one reference, you'll be redirected straightaway to it. If there is a list of references, you can choose which one you want to navigate to.

Cross-Language Navigation

If your code file (such as d.ts) contains a declaration of or a reference to a C/C++ function, you can navigate to that function easily. Just right-click a symbol name and choose Go To > Implementation(s) from the shortcut menu (or press Ctrl+Alt+B; press Command+Option+B on macOS).

Code Formatting

The editor can format your code to make it easier to read according to the preset code style settings. You can also apply custom code style settings to the specified files as follows: Under File > Settings > Editor > Code Style (DevEco Studio > Preferences/Settings > Editor > Code Style on macOS), select a file type, for example, ArkTS, and customize the code style settings.

You can use shortcut keys Ctrl+Alt+L (Option+Command+L on macOS) to quickly format selected code snippets.

To exclude certain code snippets from the automatic formatting scope, perform the following:

  1. Choose File > Settings > Editor > Code Style (DevEco Studio > Preferences/Settings > Editor > Code Style on macOS), click Formatter, and select Turn formatter on/off with markers in code comments.

  2. To exclude a code snippet from formatting, add //@formatter:off and //@formatter:on to the start and end of the code snippet, respectively.

If the project already has a code-linter.json5 file configured, right-click the file and select Apply CodeLinter Style Rules. The code formatting rules will then be consistent with the rules defined in the configured code-linter.json5 file. For details about how to configure the code-linter.json5 file, see Configuring Code Check Rules.

Code Folding

You can collapse or fold blocks of code while you are not working on them. To quickly collapse or expand a block of code, just click the collapse or expand button on the left sidebar of the editor. Better yet, you can right-click a block of code and choose how you want to collapse it, including Collapse, Collapse Recursively, and Collapse All.

Code Comments

You can press Ctrl+/ (Command+/ on macOS) to quickly comment out a selected block of code. Press Ctrl+/ (Command+/ on macOS) again to uncomment.

Code Structure Tree

Press Alt+7 or Ctrl+F12 (Command+7 on macOS) to open the code structure tree, where you can easily locate code elements, such as global variables, functions, class member variables, and methods, and quickly navigate to the corresponding code in the editor.

Code Usage Search

With the Find Usages feature, you can search for the references of a code element, such as a variable, function, or class, throughout the codebase. This is useful for subsequent code refactoring and can greatly improve development efficiency.

Usage: Right-click the code element for which you want to find usage, and choose Find Usages from the shortcut menu or press Alt+F7 (Option+F7 for macOS). Click the icon to view where the variable is assigned, and click the icon to see where the variable is referenced.

Code Comment Generation

An easy-to-use code comment generator is in place to help you quickly create comments for your code. In front of the code block of a function definition, enter /** and press Enter to insert a commented line.

In C++ files you can also enter //! and press Enter to quickly generate comments.

Code Search

You can navigate through code by navigating between symbols, classes, or files. You can examine the call and type hierarchies and explore the structure of source files. Just press Shift twice to open the code search page, and enter the keywords in the search box. The search results are displayed in the lower part of the page in real time. Click a search result to go to where it is located.

Quickly Accessing API and Component Reference Documents

Since DevEco Studio 6.0.2 Beta1, the Show in API Reference feature supports shortcut key access.

The editor provides instant access to reference documents for ArkTS/JS APIs or components, without the hassle of navigating to a specified web page.

Hover the mouse over an API or a component to display a popup with information about its parameters and other details across different API versions. To obtain the complete API reference document, click Show in API Reference in the lower-right corner of the popup, or right-click an API or a component and choose Show in API Reference from the shortcut menu (keyboard shortcut: Alt+A on Windows; Option+A on macOS).

The API reference documents integrated in DevEco Studio may not be up to date. To obtain the latest version, see HarmonyOS API References.

In the popup, the following information is available:

  1. Whether the API requires any permission declaration and whether it is available only in the test framework.
  2. Status of the API: The deprecated tag indicates that the API is no longer recommended and may be removed in a future release. For such an API, use the API marked useinstead.

Optimize Imports

The editor's Optimize Imports feature helps you remove unused imports and merge or sort imports based on preset rules. Select a file or directory and press Ctrl+Alt+O (Control+Option+O on macOS), or choose Code > Optimize Imports from the menu bar.

To modify the optimization settings, choose File > Settings (DevEco Studio > Preferences/Settings on macOS) > Editor > Code Style and select a programming language (ArkTS is used as an example here). On the Imports tab page, set the fields based on your needs.

API Change Query

Starting from version 5.1.0 Release, DevEco Studio provides the capability to query and compare behavioral changes in the ArkTS APIs used by the current project since a specified SDK version, along with adaptation guide links to facilitate code modification.

Since DevEco Studio 5.1.1 Release, this feature supports C APIs and provides the cross-version change query capability.

Since DevEco Studio 6.0.2 Beta1, a scan progress indicator has been added. The specific locations of changed APIs in your code can be viewed in Code Location.

Since DevEco Studio 6.1.0 Beta2, the API change query feature has been integrated with CodeGenie's quick Q&A. CodeGenie provides modification suggestions for incompatible APIs based on your code files and change documents, and supports API change type filtering.

[h2]Constraints

[h2]Procedure

Perform the following steps if DevEco Studio 6.0.0 Release or later is used:

  1. On the menu bar, choose Tools > API Change Assistant. On the API Change Assistant tab, select the SDK version range for comparison and click Start Scan to view the API changes by module. During scanning, a progress bar is displayed, indicating the scan progress.

    The API change query feature uses the selected starting version as a baseline to check for behavioral changes in the APIs used in the current project. For example, when you select SDK versions from 5.0.0(12) Release to 6.0.0(20) Release, the query covers changes introduced between 5.0.1(13) Beta3 and 6.0.0(20) Release, compared with the baseline of 5.0.0(12) Release.

  2. Click the code location in the scan results to jump directly to the corresponding code. For details, click the change description highlighted in blue to access the version description document. After modification, click to mark the item as fixed. Use the following options to search or filter the API change scan results, and export the data:

    • Search: Enter an API name or a file path in the search box to find specific results.
    • API Version: Set this field to filter the scan results by the selected API version.
    • Language: Set this field to filter the scan results by programming language (ArkTS or C).
    • API ID: Set this field to filter the scan results by API ID.
    • API Change Type: Set this field to filter the scan results by API change type. The options are: All, UX visual Layout Change, UX Interaction Behavior Change, API Behavior Change, API Change Deprecation, and API Definition Change.
    • Fix Status: Set this field to filter the scan results by their fix status (All, Fixed, or Unfixed).
    • Scan Again: Restart the scanning process.
    • Export: Export the API change scan results to the local device.
    • Settings: Configure the maximum heap memory size (in MB) allocated for API scanning. The default value is 3072. Adjust this value to speed up scanning for large, code-heavy projects.
    • Code Location: This panel shows the exact locations of changed APIs in your code files. Click any entry to navigate directly to that line of code. Click Quick Ask to the right of a location to open CodeGenie. In the CodeGenie dialog, click Send. CodeGenie will provide suggestions for modifying incompatible APIs based on the code file and change document. If multiple changes exist in the file, CodeGenie provides suggestions for all incompatible APIs based on all change points.

    After CodeGenie is opened via Quick Ask, only the HarmonyOS Ask agent is available for quick Q&A.

Perform the following steps if a version earlier than DevEco Studio 6.0.0 Release is used:

  1. On the menu bar, choose Tools > API Change Assistant. On the API Change Assistant tab, select the SDK version range for comparison and click Start Scan to view the API changes by module.

    The API change query feature uses the selected starting version as a baseline to check for behavioral changes in the APIs used in the current project. For example, when you select SDK versions from 5.0.0(12) Release to 6.0.0(20) Release, the query covers changes introduced between 5.0.1(13) Beta3 and 6.0.0(20) Release, compared with the baseline of 5.0.0(12) Release.

  2. Click the code address in the Code Location column to go to the corresponding code writing location. For more guidance, click the link in the Guidance link column to go to the version description document for details.

  3. Click Export, select the location for storing API change data, and export the data. You can click Scan Again for rescan. You can set the maximum heap memory size allocated for API scanning via the Settings button. If the scanning is slow for large projects, increase this value from its default of 3072 MB.

Navigation Between Parent and Child Classes

In the editor you can quickly navigate to the parent or child class of the selected interface, class, method, and property. You can navigate to its parent or child interface or class by clicking the corresponding gutter icons on the left of the code editing area. If there are multiple inheritance relationships, simply select the desired interface or class from the list presented in the popup window.

The preceding features under Gutter Icons are enabled by default. To enable or disable these features, choose File > Settings (DevEco Studio > Preferences/Settings on macOS) > Editor > General > Gutter Icons from the menu bar, and select or deselect the check boxes as needed.

Inheritance Hierarchy View

In the editor you can examine the hierarchy of interfaces and classes. Select the desired class or interface name or hover the cursor over it, then press Ctrl+H (Control+H on macOS) or choose Type Hierarchy on the Navigate tab page. In the displayed Hierarchy window, view the inheritance hierarchy of the class or interface.

The table below describes the icons in the Hierarchy window.

Icon

Description

Displays the parent and child classes of the selected class.

This feature does not support the query of inheritance relationships.

Displays the parent class of the selected class or interface.

Displays the child class of the selected class or interface.

Sorts all sibling elements in the inheritance structure tree alphabetically.

Refreshes the inheritance structure for all classes or interfaces.

Enables you to navigate to the code location of a class or interface in the editor with a single click of the class or interface name in the structure tree. By default, a double-click is needed for the navigation.

Expands or collapses the inheritance structure.

Locks the current Hierarchy window to stay on top of the editor window.

Exports the inheritance structure of the selected class or interface to a text file.

Closes the window.

View Binding Relationships of ArkUI Component State Variables

Since 26.0.0, the editor allows you to view the binding relationships of state variables. Right-click the decorator or variable attribute definition and choose Show State Variable Binding Relationship from the shortcut menu. In the State Variables dialog box, view the binding relationships of ArkUI component state variables.

The following types of state variable binding relationships are currently supported:

The table below describes the icons in the State Variables window.

Icon

Description

Refreshes all state variable relationships.

Jumps to the previous node.

Jumps to the next node.

Expands or collapses state variable relationships.

Enabling Inlay Hints

Since DevEco Studio 6.0.0 Beta2, enabling inlay hints during editing provides code description information such as parameter names and types, improving code readability.

Go to File > Settings (DevEco Studio > Preferences/Settings on macOS) > Editor > Inlay Hints, configure and check the variable name, property, parameter, and return value types you want to display, and click OK for the settings to take effect.

Copy Reference

Copy reference is supported since DevEco Studio 6.0.0 Beta2. On the editing page, select code lines or names of classes, methods, parameters, and variables, right-click and select Copy / Paste Special > Copy Reference. This will automatically copy the address of the definition. Once copied, the address can be searched in the search box that pops up by a double-press on Shift, helping you quickly locate the definition of the API.