Skip to content

Conversation

@JohnnyMorganz
Copy link
Contributor

This PR implements a new typeSpans field on ToStringResult to record which TypeId produced parts of the substring in the stringified output. This enables tooling to decompose the result and map positions in the type string back to their source types, powering features such as Go-To-Definition in Inlay Hints.

typeSpans is a vector of {startOffset, endOffset, typeId} within the string. We only populate spans for named types, i.e. extern types and named table / metatable types. The rest of the type kinds are ignored.

Special attention needs to be given for UnionType and IntersectionType. When stringifying these compound types, it works by resetting the state.result.name to an empty string before stringifying the part, then recomposing all the parts back to the full string. This can also include a sorting step.

We need to correct the typeSpans during this approach. We introduce a new ElementResult that stores the resulting string and set of spans for each part. Then when reconstructing, we offset each span by the current size of state.result.name.

These changes are gated behind the FFlag LuauToStringDecomposition

Closes #1591


// Records which TypeId produced each substring of the output. Only recorded for named types
// Each entry: {startPos, endPos, typeId}
std::vector<std::tuple<size_t, size_t, TypeId>> typeSpans;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to define a struct, std::tuple rarely works out long-term.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a decomposition of types when a type is stringified using Luau::toString(ty)

2 participants