% chapter-02.tex

\chapter{Background and Definitions}

\section{Context}
\label{contextdef}
Unless otherwise specified, this thesis assumes that a programmer copied a file
and pasted it in another location, either with the same name or with a different
name. The copied file is called the original file, and the pasted file is called
the current file. In most scenarios, the current file is also the file being
edited.

CSeR treats copying a file as a special case of copying a code block in which no
specific position is provided. Therefore, features that apply to files also
apply to code blocks, and vice versa. Positions in the current file may be called
current positions, while positions in the original file may be called original
positions.

\section{Change}
\label{changedef}
A change is usually an AST-level change from the original file to the current
file. Adding spaces within a line or between lines does not trigger a change.
Adding, deleting, or updating comments is also not considered a change because
CSeR does not track comment changes. More technically, CSeR relies on the default
implementation of ASTMatcher in JDT.

\section{Correspondence}
\label{correspondencedef}
In CSeR terminology, two AST nodes correspond when they were identical during the
copy-and-paste operation that formed the clone, and any later difference between
them is due to user editing.

\section{User-edit}
\label{usereditdef}
A user edit is any editing action, ranging from typing or deleting a single
character to inserting, replacing, or deleting a block of code or an entire file.

\section{Insert, Delete, Update operations in CSeR}
CSeR's edit operations are defined within the context of the AST. Adding a new
AST node is considered an insert, removing an AST node is considered a delete,
and modifying an existing AST node is considered an update.

These operations differ from ordinary text insert, delete, and update
operations. For example, inserting a single character is not necessarily an
insert in CSeR. In most cases, it is an update, because the smallest node that
can be inserted must be at least three characters long, such as a pre- or
post-increment expression with a one-character variable name.

\section{Position}
\label{positiondef}
In Eclipse terminology, positions describe text ranges in a document. Positions
are adapted as changes are applied to that document. A text range is specified by
an offset and a length. Positions can also be marked as deleted. Deleted
positions no longer represent valid text ranges in the managed document.

\section{Position Updaters}
\label{positionupdatersdef}
Region updaters are listeners that respond to position-change events. Whenever
an event changes regions in a document, CSeR's database must be updated with the
new information. Region updaters perform this update for CSeR.

\section{Anchor AST}
\label{anchorastdef}
The anchor AST is usually the immediate parent of the AST node being considered.
This information is crucial for statements and body declarations. The anchor AST
of a statement is usually a Block, while the anchor AST of a body declaration is
usually a TypeDeclaration. CSeR uses this information to distinguish between
operations at the statement level and operations at the body-declaration level.

\section{Statements}
\label{statementsdef}
This thesis uses the same definition of Statement nodes as JDT. Statements are
generally well-formed AST nodes that are direct children of a body node.
Statements can occur inside a method, inside a static block in a class, or inside
another block of statements.

\section{Body Declarations}
\label{bodydeclarationsdef}
This thesis uses the same definition of BodyDeclaration nodes as JDT. Body
declarations are generally well-formed AST nodes that are direct children of a
TypeDeclaration node. Type declarations can occur as inner classes inside a
method, or as field or method declarations inside a class.

\section{Nodes}
\label{nodesdef}
Nodes represent a finer level of division below the statement node. It is
practically impossible to visit every node that can compose a statement, so the
implementation uses a smaller set of nodes that programmers commonly change. In
practice, these nodes can be expressions such as variable names and string
literals, single-variable declarations such as method parameters, or type
parameters used in method calls.

\section{Block}
\label{blockdef}

A Block is the AST node that corresponds to a block of statements. It is usually
a sequence of statements enclosed in \{ and \}.

\section{Scope}
\label{scopedef}

The meaning of scope depends on the context. For statements, the scope is the
Block under consideration. For nodes, the scope is the node under consideration.
For methods and fields, the scope is the entire class.

\section{Action}
\label{actiondef}

An action is the way a user performs an edit. Different programmers use different
editing styles: some type instead of copying and pasting small code blocks, some
delete with the Delete key, some use Backspace, and others cut text to delete it.
Here, the question is how the programmer made the change, not what change was
made.

\section{Goal}
\label{goaldef}

A goal is the intended code change. The goal is independent of how the change was
made. Examples include changing a variable name, changing a class name, or adding
another parameter to a method.

\section{Infix Expressions}
Infix expressions have the form operand-operator-operand. The operator can be an
arithmetic, Boolean, assignment, or dot operator. Both operands are expressions.

\section{JDT}
\label{jdtdef}
Java Development Tools\footnote{http://www.eclipse.org/jdt} provides the Eclipse
plug-ins that enable Java application development in the Eclipse IDE. JDT also
provides APIs for processing Java ASTs and related features. CSeR makes extensive
use of these APIs.

\section{ CSeREditor}
\label{CSeReditordef}
CSeREditor is a superset of the Eclipse JavaEditor and the CSeR tools. For
consistent CSeR results, all relevant operations should be performed in
CSeREditor.

\section{PositionList}
\label{positionlistdef}
PositionList is a data structure containing positions. Positions are ordered in
this structure, so relationships such as ``after,'' ``before,'' and ``smallest''
can be defined within it.

\section{Session}
\label{sessiondef}
The meaning of session depends on the context. In the context of the CSeR
application, a session begins when Eclipse starts and ends when Eclipse closes.
In the context of a statement, a session refers to the block in which the user is
working. For example, when a user edits variables and method calls inside a
single method block, the user is working in one session. When the user creates
another block, such as a for loop or if statement, a new session begins.

\section{Levenshtein distance}
\label{levenshteindistancedef}
Levenshtein distance is a metric for measuring the difference between two
sequences. For example, the distance between ``kitten'' and ``sitten'' is 1,
while the distance between ``kitten'' and ``sitting'' is 3, because at least
three edits are required.

\section{Document}
\label{documentdef}
An IDocument represents extensible content and allows clients to set and
manipulate that content. On each document change, all registered document
listeners are informed exactly once.
