Qtcreator qml code model

From Qt Wiki
Jump to navigation Jump to search

QML and ECMAscript support in QtCreator

This document contains implementation notes about syntactic and semantic support for QML and ECMAscript programming languages in the QtCreator development environment.

This support includes all the tools that help the developer in writing and reading source code: syntax highlighting, completion, code navigation, code generation and formatting. Debugger and profiler are not included in this definition, as they are plugins on their own. Most of the tools described here are defined in the "qmljs" library and used by "qmljstools" and "qmljseditor" plugins.

The syntactic and semantic support is provided by creating a model of the actual code. Such model is referred as "QML code model" or simply "code model". Note that the same code model works also for ECMAscript. The code model contains all the information needed to assist the development of QML applications. Some of these information are static, and depends only by the language itself, like the syntax or the set of built-in types. Others are dynamic, like the current scope or the set of imported libraries. It follows that the code model is constantly updating, waiting for requests from user and monitoring every update in its sources.

The primary source of information for the code model are QML source files, that are parsed into an Abstract Syntax Tree from which a snapshot is built. Additional sources are "qmltypes" files, that contains types description: they are used for extensions written in C++ or as alternative to source files.

The code model is build in different phases from different sources: "qmltypes" files are created by the qmlplugindump tool (distributed with Qt); when QtCreator starts, built-in types data are collected; when a project is opened all the needed plugins are searched, and all the sources are monitored; when a file is opened, its source code is parsed. For a detailed description of gathering types information see QML type info in Qt Creator.

Once the code model is built, it can be queried by the other tools: the syntactic and semantic checkers for highlighting errors and warnings, the assistant for code completion, the formatter for proper indentation, or the designer for code generation.

Components

The QML language is defined in the "qtdeclarative" module shipped with Qt. Such modules includes all the tools needed to develop and run QML application. Two of these tools are particularly relevant for QtCreator: qmlplugindump and qmlimportscanner. The former is an essential tools that process a QML module and produces a file, usually named "plugins.qmltypes" that contains all the information needed by the code model. In this way, a library can be used by the code mole without distributing its source code. The second tool produce a list of import of a given QML file, and it is used internally by qmlplugindump.

The biggest part of the code model is implemented in the "qmljslib" inside QtCreator. This library contains its own copy of the QML parser and implements all the concept used to build the code model: document, context, scope, link, bind. It also contains a syntactic and semantic checker. For more details about qmljslib see Qmljslib components.

The "qmljstools" plugin handles the user preferences and provides tools for indenter, locator and semantic info.

The "qmljseditor" plugin provides the syntactic highlighter, completion and help.