ViewBinding Navigation support
Goal
Provide navigation between Android layouts (res/layout/*.xml) and their usage through generated ViewBinding.java in Kotlin.
Scenarios
| LSP |
From |
To |
| textDocument/definition |
type/import FooBarBinding (Kotlin) |
start of the layout XML file |
| textDocument/implementation |
type/import FooBarBinding (Kotlin) |
the generated *Binding.java class |
| textDocument/implementation |
tag <TextView>/<com.x.Custom> (XML) |
the view class definition |
| textDocument/references |
binding.field (Kotlin) |
all Kotlin usages of the field |
| textDocument/definition |
binding.field (Kotlin) |
@+id/field XML declaration |
| textDocument/references |
@+id/field (XML) |
all Kotlin usages of the field |
| Hover |
binding.field |
val field: <ViewType> |
Resolution
- Without a build — show error (build required)
- After a build — real field names and types come from generated Java. Special case: a root-view field in
bind() loses its R.id → recover resource_id by field name from XML.
Resolving the binding class for a position in a file
qualified, unqualified and contextual (like this, apply, it, etc)
Indexing basic
- Open Kotlin (contains
import *.databinding.*Binding): find generated Bindning.java -> resolve .xml layout and process them (async)
- Open XML layout: -> idk; hard to resolve binding class
- Change/Create XML in res/layout: index_layout_file (reindex)
- Delete XML: cleanup
layout_file_to_binding_class(id) = camelCase,snake → Pascal + "Binding.java"
view_id_to_field(id) = camelCase, snake → camelCase
is_layout_xml_path(path) = path contains "/res/layout" and p endswith ".xml"
Skip Legacy
- Data-binding
- Kotlin synthetics (
kotlinx.android.synthetic).
- Resolution without a layout file present (a pure Java binding with no XML is not indexed)
ViewBinding Navigation support
Goal
Provide navigation between Android layouts (
res/layout/*.xml) and their usage through generated ViewBinding.java in Kotlin.Scenarios
FooBarBinding(Kotlin)FooBarBinding(Kotlin)*Binding.javaclass<TextView>/<com.x.Custom>(XML)binding.field(Kotlin)binding.field(Kotlin)@+id/fieldXML declaration@+id/field(XML)binding.fieldval field: <ViewType>Resolution
bind()loses itsR.id→ recoverresource_idby field name from XML.Resolving the binding class for a position in a file
qualified, unqualified and contextual (like
this,apply,it, etc)Indexing basic
import *.databinding.*Binding): find generatedBindning.java-> resolve.xmllayout and process them (async)Skip Legacy
kotlinx.android.synthetic).