iljapostnovs / VSCodeUI5Plugin

Visual Studio Code Extension for UI5 Development
Apache License 2.0
61 stars 6 forks source link

SAPUI5 Extension

TestAndBuildBadge

Any support is highly appreciated!


Check the blog to get some ideas for developing with TS!


This plugin contains perks for UI5 developers.

Before you start working with the plugin, it will be useful to set formatOnSave setting in VSCode preferences:

"editor.formatOnSave": true

As well it's recommended to install e.g. Prettier extension for JS/TS files formatting. The reason for it is described in Known issues

Make sure that you have excludeFolderPattern property set correctly for ui5parser. This property is critical if you have SAPUI5 libraries or any other sources which should not be parsed in your workspace.


v1.7.0 update

Got new XML errors with ids and event handlers? Check out UI5 Linter Readme.

Have any ideas on how to improve MeaningAssumption variable determination? Create Feature request

Summary


Configuration

VSCode extension is using UI5 Parser and UI5 Linter packages, because of that configuration is splitted into three parts:

  1. UI5 Parser configuration. Contains parser related configuration, e.g. UI5 Version, URL for standard library metadata preload, exclude folder patterns etc. Configuration can be done in package.json or any of rc file types, e.g. .ui5pluginrc.

Important! If ui5parser related entries were changed in the package.json or any other rc file, VSCode should be reloaded manually

  1. UI5 Linter configuration. Contains linting related configuration, e.g. linter exceptions, severity, usage etc. Configuration can be done in package.json or any of rc file types, e.g. .ui5pluginrc.
  2. VSCode preference entries. Contains VSCode specific configuration.
VSCode Preference Description
ui5.plugin.jsCodeLens Should javascript CodeLens be enabled
ui5.plugin.jsReferenceCodeLens Should javascript Reference CodeLens be enabled
ui5.plugin.xmlCodeLens Should xml CodeLens be enabled
ui5.plugin.propertiesCodeLens Should properties CodeLens be enabled
ui5.plugin.signatureHelp Should UI5 Signature Help be enabled
ui5.plugin.moveDefineToFunctionParametersOnAutocomplete Should sap.ui.define class names be moved to function parameters on sap.ui.define autocomplition
ui5.plugin.codeGeneratorStrategy Strategy which should be used for code generation. E.g. you will get 'var' with ES5 strategy and 'let' with ES6 strategy when using override completion items.
ui5.plugin.UMLDiagramGenerationStrategy Which format should be used for UML diagram generation
ui5.plugin.addI18nTextLengthLimitation Should text length be added to i18n text id (e.g. #YMSG,14)
ui5.plugin.textTransformationStrategy Case type which should be used for i18n text id proposal
ui5.plugin.askUserToConfirmI18nId Should user confirm proposed i18n id
ui5.plugin.addInheritedPropertiesAndAggregations Should inherited properties/aggregations be added in XML completion items
ui5.plugin.addTagAttributes Should tag attributes be added in XML completion items
ui5.plugin.addTagAggregations Should tag aggregations be added in XML completion items
ui5.plugin.tsEventModule Event module for Typescript, which will be used when creating event handler from XML
ui5.plugin.tsEventType Event type for Typescript, which will be used when creating event handler from XML. (e.g. type of sap/ui/base/Event is Event).
ui5.plugin.insertManagedObjectModule Module which will be used for automatic insertion on custom class creation
ui5.plugin.insertControllerModule Module which will be used for automatic insertion on custom controller creation
ui5.plugin.xmlFormatterTagEndingNewline Should XML Formatter put tag ending (">", "/>") in newline
ui5.plugin.xmlFormatterSpaceAfterSelfTagEnd Should XML Formatter leave a space between the last attribute and the self tag end ("/>")
ui5.plugin.umlGenerationPath Path for file generated by UI5: Generate UML Class diagram for whole project command. Relative to project folder. Example: ../diagram/ClassDiagram.pu
ui5.plugin.XMLFileInterfacePath Path for file generated by UI5: (TS) Generate types for XML files (id to class mapping) command. Relative to project folder. Example: ./types/ViewFragmentIds.d.ts
ui5.plugin.generateXMLFileInterfacesOnSave Should UI5: (TS) Generate types for XML files (id to class mapping) command be executed on XML file save or not. Works only if ui5.plugin.XMLFileInterfacePath is set. Works only for TS projects.
ui5.plugin.TSODataInterfacesPath Path for file generated by UI5: (TS) Generate interfaces for OData entities command. Relative to project folder. Example: ./types/ODataTypes.d.ts
ui5.plugin.TSODataInterfacesFetchingData URL, username and password for UI5: (TS) Generate interfaces for OData entities command.
ui5.plugin.massTSODataInterfacesFetchingData Array of URL, username and password for UI5: (TS) Generate interfaces for OData entities command (Mass).
ui5.plugin.ERDiagramPath Path for file generated by UI5: Generate ER diagram from metadata.xml command. Relative to project folder. Example: ../diagram/ERDiagram.pu
ui5.plugin.JSTypeDefDocPath Path for file generated by UI5: Generate JS typedef JSDoc from metadata command. Relative to project folder. Example: ./model/ModelTypedef.js
ui5.plugin.globalConfigurationPath Absolute path to global any_file.json configuration file, which is used by UI5 Parser and UI5 Linter. Priority of ui5 config determination: local package.json -> global any_file.json -> default value. Example: C:\\Users\\MyUser\\Documents\\ui5config.json
ui5.plugin.idGenerationFormula Formula for id generation.
ui5.plugin.eventHandlerGenerationFormula Formula for event handler name generation.
ui5.plugin.generateIdsCommandData Data for ui5plugin.generateIds command. Contains array of classes to be excluded from ID generation and a flag if IDs should be generated for Elements as well, or for Controls only
ui5.plugin.bulkExportToi18nCommandData Data for ui5plugin.bulkExportToi18n command. Contains data about which proprties should be exported to i18n.only

Completion Items

XML

XML Completion Items for UI5 Controls.

Related preference entries:
ui5.plugin.addInheritedPropertiesAndAggregations
ui5.plugin.addTagAttributes
ui5.plugin.addTagAggregations
ui5.plugin.idGenerationFormula
_ui5.plugin.eventHandlerGenerationFormula

XMLCompletionItems
XML Completion Items for properties, aggregations, associations and events
DynamicXMLCompletionItems

JS/TS

sap.ui.define

Strings for import in sap.ui.define are provided.
UIDefine

Control ID Completion Items

IDs from the corresponding view of the controller are provided for view.byId or controller.byId method
GetView

Dynamic Completion Items

Completion items which are generated dynamically depending on current variable type or method return value type. Trigger character - dot.
DynamicCompletionItems

Manifest.json

Schema for manifest.json properties is provided.

ManifestCompletionItems


Method Definitions

Definitions for custom methods are provided.

Hotkey: Ctrl + Left Click

Definition


XML Event Handler Definitions

Definitions for event handlers are provided.

Hotkey: Ctrl + Left Click

Definition


CodeLens

CodeLens for Internalization Texts (translations), overriden methods, event handlers and member references is provided DynamicCompletionItems

Related preference entries:
ui5.plugin.jsCodeLens
ui5.plugin.jsReferenceCodeLens
ui5.plugin.xmlCodeLens
ui5.plugin.propertiesCodeLens


XML Diagnostics

See UI5 Linter for reference

DynamicCompletionItems


JS/TS Diagnostics

See UI5 Linter for reference

DynamicCompletionItems


Properties (i18n) Diagnostics

See UI5 Linter for reference


Code Action Provider

Code Actions for UI5 modules import and inserting non-existent methods in .js/.ts files are provided.
Code Actions for creating event handlers in controllers from xml views/fragments are provided.
Code Action for renaming event handlers from xml views/fragments is provided.

Hotkey: Alt + Enter

CodeActionsProvider CreateMethodCodeActionsProvider

Helpful for TS! Check out ui5.plugin.tsEventType preference entry. Handy if UI5 Version 1.115.1+ is used, because the preference entry can be changed to {tsEvent}, in such case the correct event will be added automatically. Example: ListBase$SelectionChangeEvent will be generated, if Create method action will be selected on non existant event handler in XML view sap.m.List -> selectionChange event

Image

Image

Related preference entries:
ui5.plugin.tsEventModule
ui5.plugin.tsEventType


JS and XML Hover Provider

Information on hover is provided.

HoverProvider


XML Formatter

XML Formatter is available.

XMLFormatter

Related preference entries: ui5.plugin.xmlFormatterTagEndingNewline
ui5.plugin.xmlFormatterSpaceAfterSelfTagEnd


JS/TS Rename Provider

Rename provider for js/ts is provided.
The provider renames all references to the class for JS projects, and all references for the views/fragments for both TS/JS projects.

JSRenameProvider


Commands


Move sap.ui.define to parameters

Hotkey: F5

Related preference entries: ui5.plugin.moveDefineToFunctionParametersOnAutocomplete

UIDefine


Export to i18n

Set your position to the string you want to export to i18n.properties file and execute command. Works both in XML and JS files.

Info! If there is no selection, whole string will be exported. If there is a selection, only the selected part of the string will be exported.

this.getBundle() method which returns ResourceBundle should be defined in Controller/BaseController

Related preference entries:
ui5.plugin.askUserToConfirmI18nId
ui5.plugin.addI18nTextLengthLimitation
ui5.plugin.textTransformationStrategy

Hotkey: F4

ExportToI18n


Export to i18n (bulk)

Open view or fragment and execute command. Works for XML files only.

Important! There is no info provided by SAP regarding which property should be exportable to i18n, that is why it is not possible to fully automate this process. This command exports only properties, which are configured to be exported. If there are some properties which are not exported, but should be, it is possible to adjust ui5.plugin.bulkExportToi18nCommandData VSCode preference entry. If you find yourself editing the preference entry, please open a ticket in github and share what was added, so it could be included into default value and work for everyone.

Related preference entries:
ui5.plugin.bulkExportToi18nCommandData

BulkExportToI18NCommand


Switch View/Controller

Goes to view from controller and to controller from view
If somebody uses MVC, the command actually switches between Model (Default model, which is set as this.getView().setModel(oModel) in the controller) View and Controller

For TS projects it is possible to add @ui5model JSDoc to the class, which should have a class name of the model to which the command will switch to. It will improve the performance issue, because reading type of default model might take seconds. Example:

/**
 * My Controller JSDoc
 * @ui5model {com.test.mvc.master.model.MyMasterModel}
 */

Hotkey: F3

SwitchViewController


Insert Custom Class name

Inserts the class name into current position

Hotkey: F6

InsertCustomClassNameCommand


Clear Cache

Clears cache with SAPUI5 lib metadata


UML Class Diagram generation

UML Class diagram generates for the project of the currently opened file.

Command name: UI5: Generate UML Class diagram for whole project

Also it is possible to generate ER diagram for opened metadata.xml file.

There are two ways to generate ER diagram:

  • Open metadata.xml file, execute command UI5: Generate ER diagram from metadata.xml
  • Execute command UI5: Generate ER diagram from metadata.xml and enter url to metadata.xml

It is possible to select in preferences which type of diagram to generate: DrawIO or PlantUML.
However, DrawIO is not supported anymore.

Recommended VSCode extensions:
DrawIO: hediet.vscode-drawio-insiders-build
PlantUML: jebbs.plantuml

UML Diagram example:
UML.png

ER Diagram example:
ERDiagram.png Related preference entries:
ui5.plugin.umlGenerationPath


JSDoc typedef generation from metadata

There is a possibility to generate typedef JSDocs from metadata

There are two ways to generate typedefs:

Related preference entries:
ui5.plugin.JSTypeDefDocPath

GenerateJSDocTypedef.gif


(TS) Generate interfaces for XML files (id to class mapping)

There is a possibility to generate mapping TS interfaces from control id to control type

Related preference entries:
ui5.plugin.XMLFileInterfacePath
ui5.plugin.generateXMLFileInterfacesOnSave

GenerateODataInterfaces.png


(TS) Generate interfaces for OData entities

There is a possibility to generate TS interfaces for OData entities

There are two ways to generate interfaces:

Related preference entries:
ui5.plugin.TSODataInterfacesPath
ui5.plugin.TSODataInterfacesFetchingData

GenerateXMLToIdInterfaces.png


(TS) Generate interfaces for OData entities (Mass)

Works the same as previous command, but can be used for multiple OData models at once. ui5.plugin.massTSODataInterfacesFetchingData preference entry should be configured for this command to work.

Related preference entries:
ui5.plugin.massTSODataInterfacesFetchingData


Generate ids for all elements in view or fragment

Command, which generates IDs for elements in views and fragments. To generate ids, open a view or fragment and execute command.

IDGenerationCommand

Related preference entries:
ui5.plugin.idGenerationFormula
ui5.plugin.generateIdsCommandData


Automatic template insertion

Inserts initial text for .js, .ts and .xml files
Extends "sap/ui/core/mvc/Controller" if file name ends with .controller.js/.controller.ts and "sap/ui/base/ManagedObject" if file name ends with .js/.ts

Hint! sap/ui/core/mvc/Controller can be changed to e.g. com/my/app/BaseController using ui5.plugin.insertManagedObjectModule preference entry

Related preference entries:
ui5.plugin.insertControllerModule
ui5.plugin.insertManagedObjectModule
> AutomaticTemplates


Automatic class name and class path renaming

Extension listens for .js/.ts file creation event (rename technically is file deletion and creation) and replaces all occurrences of class name to the new one
AutomaticClassNameReplacingOnRename


UI5 Explorer

Custom UI5 Explorer view in VSCode panel is available

  1. For JS/TS files tree view contains fields and methods
    JSTreeView

    Coloring for methods is based on lines count and references count.
    > Red color appears if there are more than 100 lines in one method
    > Orange color appears if there are more than 50 lines in one method or there are 0 references (reference count is ignored if method is overriden)
    > Green color appears for the rest of the cases

  2. For XML files tree view contains class tag list
    XMLTreeView


UI5 Project Data

Custom UI5 Project Data view in VSCode panel is available

It represents the data about the project, like UI5 Version, config path etc. It makes easier to troubleshoot the config related issues.

View is not updated automatically, in order to do that, refresh button on the top right corner should be pressed.

UI5ProjectData


Hotkeys

Hotkey Command
Alt + Enter Quick Fix Action
F3 Switch View/Controller
F4 Export string to i18n
F5 Move sap.ui.define imports to parameters
F6 Insert custom class name

Assumptions


Proxy

If HTTP_PROXY or HTTPS_PROXY environment variables are set, https://ui5.sap.com will be requested using the proxy.


Known issues

acorn-loose

acorn-loose is used as JS parser. It has issues if you have mixed spaces and tabs. Example:

function() {
     var oModel = this.getModel();
    var asd;
}

There is an unnecessary space before var oModel and acorn is parsing it incorrectly. As a result - the file will not be parsed as expected and you might not get JS completion items and get errors in xml views regarding wrong values for event handlers.
Keep your code clean, it will help you to have the plugin working correctly :)
It's highly recommended to set formatting on save in your VS Code preferences, it will help to avoid such issues.

"editor.formatOnSave": true

Standard VSCode JS Formatter is not handling all formatting issues. hookyqr.beautify extension did, but it got deprecated. It's highly recommended to install some kind of formatter, e.g. Prettier, and use as JS formatter.

"[javascript]": {
    "editor.defaultFormatter": "<YourFormatter>"
}

ui5.sap.com damaged JSON response

For some reason ui5.sap.com sometimes might return damaged JSON when requesting standard library metadata. As a result, it is possible to get such error as:
UIFiveError
To solve it, please run UI5: Clear cache command and reload VSCode.


Things to consider using TS

  1. If any .ts files are found and tsconfig.json is found, project is considered to be TS project
  2. src-gen folder is automatically excluded by extension if it's TS project. If build folder has different name, it should be added to folder exclusions in UI5 Parser configuration.
  3. Folder with builded resources should be added to exclusions of ui5parser. Check excludeFolderPatterns in package.json.
  4. Not all linters work for TS, because TS has a lot out of the box features. E.g. Wrong field/method linter works only for JS, because TS has it's own syntax analysis for that.
  5. ts-morph is used as TS parser and it has some drawbacks. When using typechecker to get type e.g. of the field or return type of the method, ts-morph might hang up for about ~10s, which is not great. However, types are crucial for Reference CodeLens/Linters, specifically for fields in order to be able to distinguish them in views/fragments. As a workaround for performance issues, typechecker is not used to get field types. Because of that only simple structure is allowed.

Examples which should work as expected:

export default class Random {
  formatterInstance = new Formatter(),
  formatterObject = Formatter
}

At the same time type detection will work if the type is specifically written, e.g.

formatter: Formatter = ...
  1. Disabling TS standard reference code lens should be considered. This extension contains its own reference code lens, which includes references to views and fragments.

Extension recommendations

"yaml.schemas": {
    "https://raw.githubusercontent.com/iljapostnovs/VSCodeUI5Plugin/master/schema/packageSchema.json": [
        ".ui5pluginrc.yaml",
        ".ui5pluginrc.yml"
    ]
}