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 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 Model
- type OpKind
- type Operation
- func AddConnection(owner, kind, from, to, name string) Operation
- func AddMember(owner, kind, name string) Operation
- func Delete(target string, cascade 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 SetRoute(target, view string, route *semantics.Route) Operation
- func SetRouteAt(decl source.Span, view string, route *semantics.Route) Operation
- func SetValue(target, value string) Operation
- type Referrer
- type RenameConflict
- type RenameOccurrence
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConnectionKinds ¶
ConnectionKinds lists the connection kinds legal in a language, sorted.
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 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 )
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 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 ¶
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 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 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 (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
}
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.