edit

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectionKinds

func ConnectionKinds(lang source.Kind) []string

ConnectionKinds lists the connection kinds legal in a language, sorted.

func MemberKindAdmittedBy

func MemberKindAdmittedBy(owner ast.Node, kind string) bool

MemberKindAdmittedBy reports whether the body of owner, a declaration an OpAddMember may name, offers a member of kind.

func MemberKindOwnerBound

func MemberKindOwnerBound(kind string) bool

MemberKindOwnerBound reports whether only some bodies offer a member of kind: `subject` belongs in a requirement or case, `part` anywhere.

func MemberKindTyped

func MemberKindTyped(kind string) bool

MemberKindTyped reports whether an OpAddMember of kind may carry a Type.

func MemberKinds

func MemberKinds(lang source.Kind) []string

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

type DocumentResult struct {
	Name    string
	Content []byte
	Applied []Applied
}

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.

func (*Error) Error

func (e *Error) Error() string

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
)

func (Failure) String

func (f Failure) String() string

String returns the lowercase name of the failure, or "unknown".

type MetadataValue added in v0.9.2

type MetadataValue struct {
	Feature string
	Value   string
}

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 or StyleFQN). View names the view whose
	// body states it about Target; empty, the annotation is inline on Target
	// and applies in every view.
	Annotation string
	View       string
	// The geometry to write, the one Annotation names; all nil clears the
	// annotation.
	Layout *semantics.Layout
	Route  *semantics.Route
	Canvas *semantics.Canvas
	Style  *semantics.Style
}

Operation is one change to make to a model's source.

func AddAccept added in v0.9.2

func AddAccept(owner, payload, typ, via string) Operation

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

func AddAssign(owner, target, value string) Operation

AddAssign inserts an assignment action (SysML.xtext:1535 AssignmentNode; formal/2026-03-02).

func AddComment added in v0.9.2

func AddComment(owner, body string) Operation

AddComment creates an operation adding a `comment` with body text to owner, "" for the root.

func AddConnection

func AddConnection(owner, kind, from, to, name string) Operation

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

func AddDocumentation(target, body string) Operation

AddDocumentation creates an operation adding a `doc` with body text to target.

func AddElse added in v0.9.2

func AddElse(owner, ref string) Operation

AddElse inserts a default target succession (SysML.xtext:1714 DefaultTargetSuccession; formal/2026-03-02).

func AddFirst added in v0.9.2

func AddFirst(owner, ref string) Operation

AddFirst inserts `first <ref>;` into an action body (SysML.xtext:1384 InitialNodeMember; formal/2026-03-02).

func AddFor added in v0.9.2

func AddFor(owner, variable, typ, collection string, body []Operation) Operation

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

func AddGuardedThen(owner, guard, ref string) Operation

AddGuardedThen inserts a guarded target succession (SysML.xtext:1708 GuardedTargetSuccession; formal/2026-03-02).

func AddIf added in v0.9.2

func AddIf(owner, condition string, body, elseBody []Operation) Operation

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

func AddImport(owner, visibility, target string, recursive, all bool, filters []string) Operation

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

func AddLoop(owner string, body []Operation, until string) Operation

AddLoop inserts a loop with an optional until condition (SysML.xtext:1615 WhileLoopNode, 1607 ActionBodyParameter; formal/2026-03-02).

func AddMember

func AddMember(owner, kind, name string) Operation

AddMember creates an operation inserting a declaration into owner.

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

func AddMetadataPrefix(target, metadataType string) Operation

AddMetadataPrefix annotates an existing declaration with a metadata prefix.

func AddNote added in v0.9.2

func AddNote(target, text string) Operation

AddNote creates an operation writing the line note `// text` above target.

func AddRequirementConstraint added in v0.9.2

func AddRequirementConstraint(owner, kind, expression, name string) Operation

AddRequirementConstraint creates an operation inserting a requirement constraint.

func AddSatisfy added in v0.9.2

func AddSatisfy(owner, requirement, by string, asserted, negated bool) Operation

AddSatisfy creates an operation inserting a satisfy usage.

func AddSend added in v0.9.2

func AddSend(owner, payload, to, via string) Operation

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

func AddTerminate(owner, occurrence string) Operation

AddTerminate inserts a terminate action with an optional occurrence (SysML.xtext:1641 TerminateNode; formal/2026-03-02).

func AddThen added in v0.9.2

func AddThen(owner, ref string) Operation

AddThen inserts `then <ref>;` into an action body (SysML.xtext:1703 TargetSuccession; formal/2026-03-02).

func AddThenMember added in v0.9.2

func AddThenMember(owner, kind, name, typ string) Operation

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

func AddVerify(owner, requirement string) Operation

AddVerify creates an operation inserting a requirement verification.

func AddWhile added in v0.9.2

func AddWhile(owner, condition string, body []Operation, until string) Operation

AddWhile inserts a while loop with an optional until condition (SysML.xtext:1615 WhileLoopNode, 1607 ActionBodyParameter; formal/2026-03-02).

func Delete

func Delete(target string, cascade bool) Operation

Delete creates an operation removing target, optionally including referrers.

func Move

func Move(target, newOwner string) Operation

Move is an operation making target a member of newOwner, "" for the root.

func Rename

func Rename(target, newName string) Operation

Rename is an operation rewriting target's declared name to newName.

func SetCanvas

func SetCanvas(view string, canvas *semantics.Canvas) Operation

SetCanvas is an operation sizing the drawing surface of view, or clearing its size when canvas is nil.

func SetLayout

func SetLayout(target, view string, layout *semantics.Layout) Operation

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

func SetLayoutAt(decl source.Span, view string, layout *semantics.Layout) Operation

SetLayoutAt is SetLayout of the element declared at decl in the document, which is how an element no qualified name reaches is placed.

func SetRoute

func SetRoute(target, view string, route *semantics.Route) Operation

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

func SetRouteAt(decl source.Span, view string, route *semantics.Route) Operation

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

func SetStyle(target, view string, style *semantics.Style) Operation

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

func SetStyleAt(decl source.Span, view string, style *semantics.Style) Operation

SetStyleAt is SetStyle of the element declared at decl in the document.

func SetValue

func SetValue(target, value string) Operation

SetValue is an operation setting target's value to the expression value.

func (Operation) DeclaredIn

func (op Operation) DeclaredIn(doc string) Operation

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
}

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.

func Apply

func Apply(m Model, ops []Operation) (*Result, error)

Apply applies every operation to m's source, or none of them, and returns the edited notation. Every refusal is an *Error naming its kind.

type Transition added in v0.9.2

type Transition struct {
	Name, From, To         string
	Trigger, Guard, Effect string
	Initial                bool
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL