Documentation
¶
Overview ¶
Package edit rewrites the source a model was parsed from. An operation names an element the way symbols name it, the spans of the parse say which bytes carry that element's value or name, and only those bytes are replaced — so comments, blank lines and indentation come back byte-identical. The edited notation is re-parsed and re-analyzed before it is returned: an edit that would make the model unreadable is refused rather than written.
Index ¶
- func ConnectionKinds(lang source.Kind) []string
- func ImportDrawio[R any](imp *view.DrawioImport, inline bool, apply func([]Operation) (R, error)) (R, error)
- func MemberKindAdmittedBy(owner ast.Node, kind string) bool
- func MemberKindOwnerBound(kind string) bool
- func MemberKindTyped(kind string) bool
- func MemberKinds(lang source.Kind) []string
- type Applied
- type Document
- type DocumentResult
- type Error
- type Failure
- type MetadataValue
- type Model
- type OpKind
- type Operation
- func AddAccept(owner, payload, typ, via string) Operation
- func AddAssign(owner, target, value string) Operation
- func AddComment(owner, body string) Operation
- func AddConnection(owner, kind, from, to, name string) Operation
- func AddDocumentation(target, body string) Operation
- func AddElse(owner, ref string) Operation
- func AddFirst(owner, ref string) Operation
- func AddFor(owner, variable, typ, collection string, body []Operation) Operation
- func AddGuardedThen(owner, guard, ref string) Operation
- func AddIf(owner, condition string, body, elseBody []Operation) Operation
- func AddImport(owner, visibility, target string, recursive, all bool, filters []string) Operation
- func AddLoop(owner string, body []Operation, until string) Operation
- func AddMember(owner, kind, name string) Operation
- func AddMetadata(owner, metadataType, name string, about []string, values []MetadataValue, ...) Operation
- func AddMetadataPrefix(target, metadataType string) Operation
- func AddNote(target, text string) Operation
- func AddRequirementConstraint(owner, kind, expression, name string) Operation
- func AddSatisfy(owner, requirement, by string, asserted, negated bool) Operation
- func AddSend(owner, payload, to, via string) Operation
- func AddTerminate(owner, occurrence string) Operation
- func AddThen(owner, ref string) Operation
- func AddThenMember(owner, kind, name, typ string) Operation
- func AddTransition(owner string, t Transition) Operation
- func AddVerify(owner, requirement string) Operation
- func AddWhile(owner, condition string, body []Operation, until string) Operation
- func Delete(target string, cascade bool) Operation
- func DrawioOperations(imp *view.DrawioImport, inline bool) []Operation
- func Move(target, newOwner string) Operation
- func Rename(target, newName string) Operation
- func SetCanvas(view string, canvas *semantics.Canvas) Operation
- func SetLayout(target, view string, layout *semantics.Layout) Operation
- func SetLayoutAt(decl source.Span, view string, layout *semantics.Layout) Operation
- func SetNote(target, view string, note *semantics.Note) Operation
- func SetNoteAt(annotation source.Span, note *semantics.Note) Operation
- func SetPicture(view string, picture *semantics.Picture) Operation
- func SetPictureAt(annotation source.Span, picture *semantics.Picture) Operation
- func SetRoute(target, view string, route *semantics.Route) Operation
- func SetRouteAt(decl source.Span, view string, route *semantics.Route) Operation
- func SetStyle(target, view string, style *semantics.Style) Operation
- func SetStyleAt(decl source.Span, view string, style *semantics.Style) Operation
- func SetValue(target, value string) Operation
- type Referrer
- type RenameConflict
- type RenameOccurrence
- type Result
- type Transition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConnectionKinds ¶
ConnectionKinds lists the connection kinds legal in a language, sorted.
func ImportDrawio ¶ added in v0.12.0
func ImportDrawio[R any](imp *view.DrawioImport, inline bool, apply func([]Operation) (R, error)) (R, error)
ImportDrawio applies an import's edits through apply, the editor of the document the view is declared in. A rename the editor refuses — the name is taken, a reference would be captured — is reported in the import's notices and the other edits are applied again, so one refused rename holds none of them back; any other refusal is the caller's. The import's edits are left as what was applied.
func MemberKindAdmittedBy ¶
MemberKindAdmittedBy reports whether the body of owner, a declaration an OpAddMember may name, offers a member of kind.
func MemberKindOwnerBound ¶
MemberKindOwnerBound reports whether only some bodies offer a member of kind: `subject` belongs in a requirement or case, `part` anywhere.
func MemberKindTyped ¶
MemberKindTyped reports whether an OpAddMember of kind may carry a Type.
func MemberKinds ¶
MemberKinds lists the member kinds legal in a language, sorted.
Types ¶
type Applied ¶
type Applied struct {
OperationIndex int
Target string
// Span is the range replaced; Len is 0 for an insertion.
Span source.Span
OldText string
NewText string
}
Applied describes one replacement. Batch spans use the original source; sequential spans use the intermediate source seen by that operation.
type Document ¶
type Document struct {
Source *source.SourceFile
ParseDiags []parser.Diagnostic
SemDiags []diag.Diagnostic
}
Document is the source of another document of a Model's index, as Index was built from it, with what its original was found to have.
type DocumentResult ¶
DocumentResult is the edited notation of one other document and what changed in it.
type Error ¶
type Error struct {
Failure Failure
Message string
// OperationIndex is the operation refused, or -1 for a whole request.
OperationIndex int
// Diagnostics are the errors behind a refusal: those of an unreadable new
// value, or those the edited notation was found to have.
Diagnostics []diag.Diagnostic
// Diagnosed is the source the Diagnostics' spans are offsets into: the new
// value's text, or the edited notation. A refusal still returns no model.
Diagnosed *source.SourceFile
// Referring names the declarations referring to a target whose delete,
// rename or move was refused, each qualified by its document when that is another.
Referring []string
// Referrers is Referring with each name told apart from its document, for a
// client that lists them by document; empty where Referring names no declaration.
Referrers []Referrer
}
Error is a refused edit: which kind of refusal it is, and the evidence for it.
type Failure ¶
type Failure int
Failure is why edits were refused. Every refusal carries one: an edit is never silently dropped.
const ( // FailureNone is no failure. FailureNone Failure = iota // FailureNoOperations is a request naming no edit. FailureNoOperations // FailureUnknownTarget is a target this model declares nothing under. FailureUnknownTarget // FailureAmbiguousTarget is a target several declarations answer to. FailureAmbiguousTarget // FailureNotValued is a target that can carry no value. FailureNotValued // FailureInvalidValue is a new value that does not parse as an expression. FailureInvalidValue // FailureInvalidName is a new name that does not lex as an identifier, or a // connection end that is not written as a feature reference. FailureInvalidName // FailureNotNamed is a target declaring no name to rewrite. FailureNotNamed // FailureRenameReferenced is a rename that references to the element would // not survive. FailureRenameReferenced // FailureOverlappingEdits is two edits covering the same bytes. FailureOverlappingEdits // FailureResultInvalid is edited notation carrying errors the original had // not. FailureResultInvalid // FailureOwnerUnknown is an add-member owner that is not declared. FailureOwnerUnknown // FailureOwnerNotNamespace is an element that cannot contain members. FailureOwnerNotNamespace // FailureIllegalKind is a declaration kind invalid for the document language. FailureIllegalKind // FailureMemberNameTaken is a member name already declared by the owner. FailureMemberNameTaken // FailureDeleteReferenced is a non-cascade delete with live references. FailureDeleteReferenced // FailureReferencedElsewhere is a delete or rename of a declaration referred // to from a document the edit may not rewrite — one the model hands out no // source for, such as a document not open in the editor or a bundled library // file — so the reference could not follow. A reference from a document the // edit may rewrite is followed instead. A move respells references in its // own document only, so any other document's reference refuses it. A layout // whose annotation is stated in such a document refuses the same way. FailureReferencedElsewhere // FailureOwnerInsideTarget is a move whose new owner is the target itself or // a declaration inside it. FailureOwnerInsideTarget // FailureMoveReferenced is a move leaving a reference no spelling can make // reach what it reached before. FailureMoveReferenced // FailureNotAView is a layout view, or a Canvas target, that is no view. FailureNotAView // FailureNotExposed is a view-local layout of an element the view does not // expose. FailureNotExposed // FailureNotDrawn is a layout of an element no rendering draws as the node // or edge the annotation positions. FailureNotDrawn // FailureNotAnnotated is a clearing of an annotation that is not there. FailureNotAnnotated )
type MetadataValue ¶ added in v0.9.2
MetadataValue is one feature binding written in a metadata body.
type Model ¶
type Model struct {
Source *source.SourceFile
Root *ast.RootNamespace
Index *symbols.Index
// ParseDiags and SemDiags are what the original was found to have, so a
// refusal reports the errors an edit introduced and not ones it inherited.
ParseDiags []parser.Diagnostic
SemDiags []diag.Diagnostic
// NewIndex hands out an index carrying the libraries the model was analyzed
// against and every other document of Index, but none under Source's name,
// for analyzing the edited notation. Nil checks syntax alone.
NewIndex func() *symbols.Index
// Indexed, when set, is called after each document the edit adds to that index,
// so the caller can keep the index's library marks as it keeps its own.
Indexed func(idx *symbols.Index, sf *source.SourceFile, root *ast.RootNamespace)
// Analysis is the options the edited notation is judged under, the same the
// original's SemDiags came from; the zero value is the default mode.
Analysis passes.Options
// Other hands out another document of Index whose notation the edit may
// rewrite when a rename or delete reaches a reference in it, or false for one
// it may not, which the edit then refuses to follow. Nil rewrites Source alone.
Other func(name string) (Document, bool)
// Documents names the other documents a reference to the edited one may be
// written in; nil reads them as Index's unmarked documents.
Documents []string
// contains filtered or unexported fields
}
Model is a parsed model to edit: the source that was read, its parse, and the index it was analyzed in.
type OpKind ¶
type OpKind int
OpKind is which source-preserving change an Operation makes.
const ( // OpSetValue sets the value of a feature that already exists. OpSetValue OpKind = iota // OpRename rewrites the name token of a declaration and the references to it. OpRename // OpAddMember inserts a declaration into a namespace. OpAddMember // OpDelete removes a declaration and its owned trivia. OpDelete // OpAddConnection inserts a connector-like usage joining two features. OpAddConnection // OpMove re-parents a declaration: the span OpDelete removes is written // where OpAddMember inserts, and the references it breaks are respelled. OpMove // OpSetLayout writes, updates or clears a DiagramLayout annotation. OpSetLayout // OpAddSatisfy inserts a satisfy requirement usage. OpAddSatisfy // OpAddRequirementConstraint inserts a requirement constraint. OpAddRequirementConstraint // OpAddTransition inserts a transition usage into a state body. OpAddTransition // OpAddVerify inserts a requirement verification into a verification objective. OpAddVerify // OpAddMetadata inserts a metadata usage. OpAddMetadata // OpAddMetadataPrefix adds prefix metadata to an existing declaration. OpAddMetadataPrefix // OpAddSequence inserts a `first`/`then` sequencing member into an action body. OpAddSequence // OpAddImport inserts an import declaration into a namespace body or the // document root. OpAddImport // OpAddDocumentation adds a `doc` to an existing declaration, or rewrites // the one it owns. OpAddDocumentation // OpAddComment inserts a `comment` element into Owner. OpAddComment // OpAddNote writes a `//` line note above Target's declaration. OpAddNote )
type Operation ¶
type Operation struct {
Kind OpKind
// Target is the element to edit, by FQN, as symbols name it. An OpSetLayout
// may give Declaration instead, the span the element is declared at, for an
// element no qualified name reaches: an unnamed one, or one declared inside
// an unnamed one. DeclarationDoc names the document the element is declared
// in — the one the span is of, or the one Target must resolve to a declaration
// of; empty means the edited document for a span and any for a Target.
Target string
Declaration source.Span
DeclarationDoc string
// Value is the new value in SysML notation, for OpSetValue.
Value string
// NewName is the new declared name, for OpRename.
NewName string
// Owner is the namespace receiving an authoring operation; empty means the
// root.
Owner string
// Declaration details for OpAddMember and OpAddConnection.
MemberKind string
MemberName string
Type string
Multiplicity string
Specializes []string
IsAbstract bool
Redefines []string
IsDefault bool
Direction string
// MetadataPrefixes are metadata types annotating a new member.
MetadataPrefixes []string
// BodyExpression is the condition a constraint-kind member states in its body.
BodyExpression string
// From and To are the ends of an OpAddConnection, written as the notation
// references features (`a.p`, `A::b`).
From string
To string
Cascade bool
// Requirement and SatisfyingFeature are the feature references of an
// OpAddSatisfy. The latter is optional.
Requirement string
SatisfyingFeature string
Asserted bool
Negated bool
// ConstraintKind, Expression and ConstraintName describe an
// OpAddRequirementConstraint.
ConstraintKind string
Expression string
ConstraintName string
// TransitionSource, TransitionTarget, TransitionName and the optional
// trigger, guard and effect describe an OpAddTransition.
TransitionName string
TransitionSource string
TransitionTarget string
Trigger string
Guard string
Effect string
Initial bool
// MetadataType, MetadataName, About, MetadataValues and Shorthand describe
// an OpAddMetadata. MetadataType also describes OpAddMetadataPrefix.
// About names elements annotated by OpAddMetadata or OpAddComment.
// Requirement is the requirement verified by OpAddVerify.
MetadataType string
MetadataName string
About []string
MetadataValues []MetadataValue
Shorthand bool
// SequenceKeyword ("first" or "then"), SequenceRef (the node a bare
// `then`/`first` names) and After (the member an OpAddSequence follows)
// describe an OpAddSequence; a `then` declaring a member reuses MemberKind,
// MemberName and Type.
SequenceKeyword string
SequenceRef string
SequenceCondition string
SequenceValue string
SequenceTarget string
SequenceVia string
SequenceUntil string
SequenceParameter string
SequenceBody []Operation
SequenceElse []Operation
After string
// Doc is the plain body text of a `doc`: the first body member of the
// declaration an OpAddMember writes, or the documentation an
// OpAddDocumentation adds to Target. DocName and DocLocale are the
// latter's optional identification and locale, and ReplaceDoc has it
// rewrite the one documentation Target owns rather than refuse.
// An OpAddComment writes Doc as its body and DocName and DocLocale as its
// identification and locale.
Doc string
DocName string
DocLocale string
ReplaceDoc bool
// ImportVisibility, ImportTarget, ImportRecursive, ImportAll and
// ImportFilters describe an OpAddImport.
ImportVisibility string
ImportTarget string
ImportRecursive bool
ImportAll bool
ImportFilters []string
// Note is the one line of text an OpAddNote writes after `// `.
Note string
// NewOwner is the namespace an OpMove moves Target into; empty means the root.
NewOwner string
// Annotation is the DiagramLayout metadata an OpSetLayout writes, by FQN
// (semantics.LayoutFQN, RouteFQN, CanvasFQN, StyleFQN, NoteFQN or PictureFQN).
// View names the view whose body states it about Target; empty, the
// annotation is inline on Target and applies in every view. A Note or a
// Picture may be stated many times over one element, so Declaration names
// the annotation itself, not the element, when one of those is rewritten.
Annotation string
View string
// The geometry to write, the one Annotation names; all nil clears the
// annotation. NoteBox is the Note annotation, Note the text an OpAddNote adds.
Layout *semantics.Layout
Route *semantics.Route
Canvas *semantics.Canvas
Style *semantics.Style
NoteBox *semantics.Note
Picture *semantics.Picture
}
Operation is one change to make to a model's source.
func AddAccept ¶ added in v0.9.2
AddAccept inserts an accept node, optionally typed and associated with a port (SysML.xtext:1442 AcceptNode; formal/2026-03-02).
func AddAssign ¶ added in v0.9.2
AddAssign inserts an assignment action (SysML.xtext:1535 AssignmentNode; formal/2026-03-02).
func AddComment ¶ added in v0.9.2
AddComment creates an operation adding a `comment` with body text to owner, "" for the root.
func AddConnection ¶
AddConnection creates an operation inserting into owner a connector-like usage of kind (`connection`, `flow`, `succession`, …) joining from to to. name may be empty for an anonymous connection.
func AddDocumentation ¶ added in v0.9.2
AddDocumentation creates an operation adding a `doc` with body text to target.
func AddElse ¶ added in v0.9.2
AddElse inserts a default target succession (SysML.xtext:1714 DefaultTargetSuccession; formal/2026-03-02).
func AddFirst ¶ added in v0.9.2
AddFirst inserts `first <ref>;` into an action body (SysML.xtext:1384 InitialNodeMember; formal/2026-03-02).
func AddFor ¶ added in v0.9.2
AddFor inserts an iteration over a collection with an optional type (SysML.xtext:1624 ForLoopNode, 1607 ActionBodyParameter; formal/2026-03-02).
func AddGuardedThen ¶ added in v0.9.2
AddGuardedThen inserts a guarded target succession (SysML.xtext:1708 GuardedTargetSuccession; formal/2026-03-02).
func AddIf ¶ added in v0.9.2
AddIf inserts a conditional action with optional else-body items (SysML.xtext:1596 IfNode, 1607 ActionBodyParameter; formal/2026-03-02).
func AddImport ¶ added in v0.9.2
AddImport inserts an import declaration of target into owner: a membership import for a qualified name, a namespace import for one suffixed `::*`, recursive or importing non-public members when recursive or all are set, and filtered by the filter expressions, each written `[<expression>]`.
func AddLoop ¶ added in v0.9.2
AddLoop inserts a loop with an optional until condition (SysML.xtext:1615 WhileLoopNode, 1607 ActionBodyParameter; formal/2026-03-02).
func AddMetadata ¶ added in v0.9.2
func AddMetadata(owner, metadataType, name string, about []string, values []MetadataValue, shorthand bool) Operation
AddMetadata creates an operation inserting a metadata usage.
func AddMetadataPrefix ¶ added in v0.9.2
AddMetadataPrefix annotates an existing declaration with a metadata prefix.
func AddNote ¶ added in v0.9.2
AddNote creates an operation writing the line note `// text` above target.
func AddRequirementConstraint ¶ added in v0.9.2
AddRequirementConstraint creates an operation inserting a requirement constraint.
func AddSatisfy ¶ added in v0.9.2
AddSatisfy creates an operation inserting a satisfy usage.
func AddSend ¶ added in v0.9.2
AddSend inserts a send node with a payload and optional receiver and port (SysML.xtext:1499 SendNode; formal/2026-03-02).
func AddTerminate ¶ added in v0.9.2
AddTerminate inserts a terminate action with an optional occurrence (SysML.xtext:1641 TerminateNode; formal/2026-03-02).
func AddThen ¶ added in v0.9.2
AddThen inserts `then <ref>;` into an action body (SysML.xtext:1703 TargetSuccession; formal/2026-03-02).
func AddThenMember ¶ added in v0.9.2
AddThenMember inserts `then <kind> <name> : <type>;` (SysML.xtext:1368 ActionNodeMember, 1703 TargetSuccession; formal/2026-03-02), declaring the member the `then` sequences to; kind is action, perform action, state, merge, decide, join or fork, and the control nodes take no type.
func AddTransition ¶ added in v0.9.2
func AddTransition(owner string, t Transition) Operation
AddTransition inserts a state transition, or an entry transition when initial.
func AddVerify ¶ added in v0.9.2
AddVerify creates an operation inserting a requirement verification.
func AddWhile ¶ added in v0.9.2
AddWhile inserts a while loop with an optional until condition (SysML.xtext:1615 WhileLoopNode, 1607 ActionBodyParameter; formal/2026-03-02).
func DrawioOperations ¶ added in v0.12.0
func DrawioOperations(imp *view.DrawioImport, inline bool) []Operation
DrawioOperations turns the edits an import of a draw.io file found into the operations writing them: each annotation stated in the body of the imported view — inline on the element with inline set — and, whatever the scope, inline on an element no qualified name reaches, as a view's body cannot name it. A moved note or picture rewrites the annotation that drew it.
func SetCanvas ¶
SetCanvas is an operation sizing the drawing surface of view, or clearing its size when canvas is nil.
func SetLayout ¶
SetLayout is an operation placing target in view — inline on target when view is empty — or clearing its position when layout is nil.
func SetLayoutAt ¶
SetLayoutAt is SetLayout of the element declared at decl in the document, which is how an element no qualified name reaches is placed.
func SetNote ¶ added in v0.12.0
SetNote is an operation adding a Note to target — in the body of view, or inline on target when view is empty; target may be the view itself, for a note about nothing in particular — or rewriting the first one it carries; a nil note clears that one.
func SetNoteAt ¶ added in v0.12.0
SetNoteAt rewrites the Note annotation declared at annotation in the document, wherever it is stated, or clears it when note is nil.
func SetPicture ¶ added in v0.12.0
SetPicture is an operation adding a Picture to the body of view, or rewriting the first one it states; a nil picture clears that one.
func SetPictureAt ¶ added in v0.12.0
SetPictureAt rewrites the Picture annotation declared at annotation in the document, or clears it when picture is nil.
func SetRoute ¶
SetRoute is an operation steering the edge target through route's waypoints in view — inline on target when view is empty — or clearing them when route is nil.
func SetRouteAt ¶
SetRouteAt is SetRoute of the edge declared at decl in the document, which is how an unnamed transition or succession is steered.
func SetStyle ¶ added in v0.9.2
SetStyle is an operation colouring target in view — inline on target when view is empty — or clearing its Style when style is nil.
func SetStyleAt ¶ added in v0.9.2
SetStyleAt is SetStyle of the element declared at decl in the document.
func (Operation) DeclaredIn ¶
DeclaredIn is op with its element declared in the named document: its Declaration a span of that document's text, its Target a name declared there.
type Referrer ¶
type Referrer struct {
// Name is the declaration as the notation names it, an anonymous one by its
// heading or keyword within its namespace.
Name string
// Document is the document declaring it.
Document string
}
Referrer is one declaration referring to the target of a refused delete or rename.
type RenameConflict ¶
type RenameConflict struct {
// Subject is the qualified name of the element being renamed.
Subject string
// NewName is the name refused.
NewName string
// Means is the qualified name NewName already means, or would at a reference.
Means string
// Site is the namespace of the captured reference; empty for a taken name.
Site string
// Occurrence indexes the captured reference among those checked; meaningful
// only where Site is set.
Occurrence int
// Ambiguity is how many elements the reference would name at once, where the
// rename would leave it ambiguous rather than reading another element.
Ambiguity int
// Hidden is set where another import brings Means under NewName beside the
// renamed element, so the name would be hidden at the reference (KerML 7.2.5.4).
Hidden bool
}
RenameConflict is why a rename is refused: what the new name would mean instead.
func CheckRename ¶
func CheckRename(r *resolve.Resolver, sem *semantics.Model, sym *symbols.Symbol, name, newName string, occurrences []RenameOccurrence) *RenameConflict
CheckRename reports the first conflict renaming sym's written name to newName would create: the declaration's own scope first, then each occurrence in order. sem selects what a respelled call would run, as the checker selects it.
func (*RenameConflict) Error ¶
func (c *RenameConflict) Error() string
Error describes the conflict, naming what the new name would mean.
type RenameOccurrence ¶
type RenameOccurrence struct {
// Ref is the reference the segment belongs to, as the document walk collected it.
Ref resolve.Reference
// Part indexes the segment in Ref.QN.
Part int
}
RenameOccurrence is one reference segment written with the name being renamed.
func (RenameOccurrence) Span ¶
func (o RenameOccurrence) Span() source.Span
Span is the segment's bytes in its document.
type Result ¶
type Result struct {
Content []byte
Applied []Applied
Others []DocumentResult
}
Result is the edited notation and what each operation changed. Content and Applied are the edited document's; Others are the other documents a rename or delete followed a reference into, in name order, and none when it reached none.
type Transition ¶ added in v0.9.2
Transition describes the transition an AddTransition inserts: an entry transition names its target alone; any other names its source and target, and optionally a name, trigger, guard and effect.