edit

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 15 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 added in v0.8.0

func ConnectionKinds(lang source.Kind) []string

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

func MemberKindAdmittedBy added in v0.8.0

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 added in v0.8.0

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 added in v0.8.0

func MemberKindTyped(kind string) bool

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

func MemberKinds added in v0.8.0

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 added in v0.8.0

type Document struct {
	Source     *source.SourceFile
	ParseDiags []parser.Diagnostic
	SemDiags   []passes.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 added in v0.8.0

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 []passes.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.
	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 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   []passes.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
	// 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)
	// 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
)

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 in this
	// document, for an element no qualified name reaches: an unnamed one, or
	// one declared inside an unnamed one.
	Target      string
	Declaration source.Span
	// 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 OpAddMember or OpAddConnection; empty
	// means the root.
	Owner string
	// Declaration details for OpAddMember and OpAddConnection. MemberName is
	// optional for a connection, which the notation lets be anonymous.
	MemberKind   string
	MemberName   string
	Type         string
	Multiplicity string
	Specializes  []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
	// 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 or CanvasFQN). 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
}

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

func AddConnection added in v0.8.0

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 AddMember added in v0.2.0

func AddMember(owner, kind, name string) Operation

AddMember creates an operation inserting a declaration into owner.

func Delete added in v0.2.0

func Delete(target string, cascade bool) Operation

Delete creates an operation removing target, optionally including referrers.

func Move added in v0.8.0

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 added in v0.8.0

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 added in v0.8.0

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 added in v0.8.0

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 added in v0.8.0

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 added in v0.8.0

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 SetValue

func SetValue(target, value string) Operation

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

type Referrer added in v0.8.0

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 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.

Jump to

Keyboard shortcuts

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