UP | HOME
CS 7140 2015-07-25

Design of Entities and Operations within a Programmers' Editor

Table of Contents

I use editors as target examples in Software Engineering courses I teach. The materials below are hence deliberately incomplete. The context of this article is Programmers' Editor

1 Meta Commentary

  1. There is always some overlap between specs and design.
  2. The driving force in
    1. Specs is "what"
    2. Design is "how"
  3. These notes implicitly reference items explained in Editors
  4. Please report errors. I am afraid it is not error-free. Ignore spaces within the = = token.

2 Design of a Buffer

  1. An edit buffer can be designed as a (singly-, doubly-, circular- …) lists, or as a real array. This is straight forward, and too simplistic if we wish large files, in the 100+ MB range.
  2. We will use a non-obious design here just because ;-)
  3. Inspired by Emacs!
  4. The buffer B has several data members.
    1. left: seq of bytes;
    2. gapz: int;
    3. right: seq of bytes;
    4. dot: cursor initialized to (:=) (0, 0);
    5. mark: cursor := (0, 0);
    6. A few more will be introduced later.
  5. We will refer the combo of left + gap + right as simply the B.buf.
  6. B.specs.content = = B.design.left + B.design.right; That is, we are splitting the content into left and right parts. Why? And where is the split? Documented later.
  7. Before and after all public methods this conditions should hold. This is a class invariant.
  8. We are using the typical OOP notation of dot to relate the spec of an entity with its design.
  9. Below we drop ".design" because that is what is focussed in this document.
  10. We require that b.gapz >= 0. This gap is an area of "unused" memory of this size where new insertions happen.
  11. Design Decision: We will not track which windows are on a given buffer.
  12. Design Clarification: The '\n' is stored. When saving a buffer, it will become '\r\n' in Windows, '\r' in Mac OS, and remain as '\n' in Linux.

2.1 Public Methods: create

  1. Create a new buffer from an existing file named fnm. This is a constructor.
  2. Lower level design details: B.lz = = size of B.left in bytes; B.rz = = size of B.right in bytes; B.gz = = same as gapz; B.sz = = sum of these three.
  3. Design Decision: Where should the dot marks be? Dot at the beginning. Mark at the end.
  4. The design below is showing an expected OS API usage. This is too low level just to show that sometimes we have to do that.
    f := sys-call-open(fnm, for-reading);  // assume exists
    b.sz = 1000;   // adjust later
    b.buf := create a grow/shrink array initially of size b.sz bytes;
    b.fnm := fnm;
    b.dot := b.mark := (0, 0);
    b.left := b.right := empty;
    while not eof(f) {
      el := read-next-line(f); // stripped off the '\n'
      b.left += el;
      b.mark.y += 1;
      b.markx := #el;
    }
    sys-call-close(f);
    
  5. Note that the b.buf is expected to grow as needed in the += el line. We expect to use a library method native to the PL. This happens in spurts. b.right is initially empty. b.gapz = = the left over area.

2.2 Public Methods: destroy, insert, delete, cut, paste

3 Design of a Window

  1. The window W has a reference to a buffer B. Several more data members are introduced later.

3.1 Public Method: Create a Window W on B

  1. This is a constructor. Must the ww and hh be "given"?
  2. dot := B.dot;
  3. mark := B.dot;
  4. ww := 80; // why 80?
  5. hh := 25; // why 25?
  6. content: seq of ww x hh bytes;
  7. W.content = = W.spec.content; This is a class invariant.
  8. Design Clarification: We should replace bytes with UTF-n considerations.
  9. Design Clarification: W.content is computed from B.content, and the anchor point of the window given by W.leftx, W.topy coordinates that are relative to the buffer B. Refer to the idea of "padding" previosuly described.
  10. 0 <= W.topy < #B.lines
  11. 0 <= W.leftx < length of the longest line of B
  12. Call on the GUI of the system to show W.

3.2 Public Method: Move Window W Right by (dx, dy)

  1. For now assume dx > 0, dy > 0, both integers. So, intuitively, we are moving the window rightwards by dx and downwards by dy. The buffer scroll behind the window remains stationary.
  2. W.leftx += dx;
  3. W.topy += dy;
  4. We must maintain W.content = = W.spec.content class invariant.
  5. Design Decision: We must decide whether to adjust the current W.content or recompute it afresh from the new W.leftx, W.topy coordinates. This decision can be based on algorithmic simplicity (which has an effect on software maintenance) or speed of computation (users don't like lag). Exercise!
  6. Call on the GUI of the system to reflect the move of W.

3.3 Public Method: Destroy Window W

  1. B.dot := W.dot; // why?
  2. B.mark := W.mark; // why?
  3. Release W

3.4 Public Method: Hide Window W

  1. Design Decision: Is this the same as Destroy Window?
  2. Design Clarification: W is released. B.content remains unchanged.

3.5 Public Method: Show Window W

3.6 Public Method: Enlarge Window W

  1. Design Clarification: W.content changes, but B.content remains unchanged.

3.7 Public Method: Shrink Window W

  1. Design Clarification: W.content changes, but B.content remains unchanged.

4 Design of a Cursor

  1. Designed literally as specified. That is, a cursor is a pair (x, y), both non-negative integers.
  2. Cursors have no independent existence. They are always part of a window or a buffer.

Copyright © 2015 • www.wright.edu/~pmateti • 2015-07-25