Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

192
LINES

< > BotCompany Repo | #1015560 // Snippet/File Editor v3 [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 6678K of libraries. Click here for Pure Java version (11874L/87K).

!7

// What are we editing?
static O editing; // File or snippet ID
sS remoteText;

sS savedText;
static EditorFrame e;
static int caretPos;
static SimpleLiveValue<O> lvEditing = new(O.class);
static int fontSize = 16;
static AutoCompletion autoCompletion;
static ReliableSingleThread rstAutoComplete = new(f installAutoComplete);
sbool installAutoComplete_first;
static Set<S> autoComplete_lastSet;
static DynamicHStack smartArea;

p-substance {
  autoRestart();
  load('savedText);
  load('fontSize);
  
  e = new EditorFrame;
  e._setFontSize(fontSize);
  rstAutoComplete.trigger();
  onChange(e.textArea, rTrigger(rstAutoComplete));
  
  addToWindowWithMargin(e, jHigherScrollPane(jfullcenter(smartArea = dynamicHStack())));
  
  e.setText(savedText);
  load('caretPos);
  setCaretPosition(e.textArea, caretPos);
  showFrame(e);
  frameIcon(e, #1101185 /*#1004655*/);
  
  addMenu(e, "New", "New text", f newText);

  addMenu(e, "Load", "Open snippet...", r {
    selectSnippetID(vf1(f _loadSnippet));
  }, "Open file...", r {
    selectFile("File to open", vf1(f _loadFile));
  });
  
  addMenu(e, "Save", mapLiveValue(func(O editing) -> S {
    editing instanceof S ? "Save snippet " + editing
      : editing instanceof File ? "Save file " + f2s((File) editing)
      : "Save" }, S.class, lvEditing), r { saveThen(null) });
  
  addMenu(e, "Transpile",
    "Save & Quick Transpile", r { transpile(false) },
    "Save & Medium Transpile", r { transpile(true) });
  addMenu(e, "Activate", "Butter-Reload", f _butterReload);
  
  addMenu(e, "View",
    "Bigger font", r { e._setFontSize(setAndSave(fontSize := e._getFontSize()+1)) },
    "Smaller font", r { e._setFontSize(setAndSave(fontSize := max(1, e._getFontSize()-1))) });
  
  O editing = cast readPersistentVar('editing);
  if (editing instanceof S) _loadSnippet_noSet((S) editing);
  else if (editing instanceof File) _loadFile_noSet((File) editing);

  hideConsole();
  
  awtCalcEvery(e, 1000, f update);
  onFrameDeactivated(e, f fullUpdate);
}

svoid update { update(windowActive(e)); }
svoid fullUpdate { update(true); }
svoid update(bool full) {
  S text = e.getText();
  setAndSaveIfChanged(savedText := text);
  setAndSaveIfChanged(caretPos := getCaretPosition(e.textArea));
    
  if (!full) ret;
  S status = remoteText != null && neq(remoteText, text) ? "Changes. " : "";
  Pair<Int, S> p = testBracketHygieneExt(text);
  status += p == null ? "Hygienic" : p.b;
  silentStatus(trim(status));
}

svoid status(S s) { silentStatus(infoBoxAndReturn(s)); }
svoid silentStatus(S s) { e.setStatus(s); }

svoid saveThen(final Runnable next) {
  fS text = getText(e.textArea);
  if (editing instanceof S) {
    status("Saving snippet...");
    thread {
      fS page = editSnippet((S) editing, text);
      remoteText = text; // TODO: check save result
      status("Saved snippet: " + page);
      awtCallF(next);
    }
  } else if (editing instanceof File) {
    status("Saving file...");
    thread {
      saveTextFile((File) editing, text);
      remoteText = text;
      status("Saved file");
      awtCallF(next);
    }
  }
}

svoid _butterReload() {
  time { butterReload((S) editing); }
  infoBox("Reloaded in " + lastTiming_format100ms());
}

svoid transpile(final bool medium) { saveThen(r { _transpile(medium) }); }

svoid _transpile(final bool medium) {
  status("Transpiling...");
  //jdisable(btnEleuReload);
  
  thread "Transpiling" {
    try {
      final Pair<Bool, S> p = transpileOnServer((S) editing, medium ? "medium" : "quick");
      awt {
        if (p.a)
          status("Transpiled OK!");
        else {
          status("Transpilation error. " + p.b);
          showText("Transpilation Error", p.b);
        }
      }
    } catch print e {
      status("Transpilation problem. " + e);
    }
    //jenable(btnEleuReload);
  }
}

svoid _loadSnippet(S snippetID) {
  _loadSnippet_noSet(snippetID);
  e.textArea.setText(remoteText);
}

svoid _loadSnippet_noSet(S snippetID) {
  remoteText = loadSnippet(snippetID);
  setAndSave('editing, snippetID);
  lvEditing.set(snippetID);
  frameTitle(e, snippetID + " - " + snippetTitle(snippetID));
}

svoid _loadFile(File f) {
  _loadFile_noSet(f);
  e.textArea.setText(remoteText);
}

svoid _loadFile_noSet(File f) {
  remoteText = loadTextFile(f);
  setAndSave('editing, f);
  lvEditing.set(f);
  frameTitle(e, f2s(f));
}

svoid newText {
  setAndSave(editing := null);
  lvEditing.set(null);
  remoteText = null;
  S lastText = e.getText();
  if (nempty(lastText)) logQuotedWithDate("saved-texts.log", lastText);
  e.setText("");
  frameTitle(e, programTitle());
}

svoid cleanMeUp { update(false); }

svoid installAutoComplete {
  if (installAutoComplete_first) installAutoComplete_first = false;
  else sleep(500); // don't update too often
  
  temp tempMiniLoadingAnim();

  Set<S> set = concatListsToSet(allJavaKeywords(), standardFunctionNames(), deepWords(getText(e.textArea)));
    
  if (eq(set, autoComplete_lastSet)) ret;
  autoComplete_lastSet = set;
  
  final new DefaultCompletionProvider provider;
  provider.addCompletions(map(func(S s) { new BasicCompletion(provider, s) }, set));
    
  swing {
    if (autoCompletion == null) {
      (autoCompletion = new AutoCompletion(provider)).install(e.textArea);
      call(autoCompletion, 'setHideOnCompletionProviderChange, false);
    } else
      autoCompletion.setCompletionProvider(provider);
  }
}

Author comment

Began life as a copy of #1015549

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015560
Snippet name: Snippet/File Editor v3 [OK]
Eternal ID of this version: #1015560/11
Text MD5: cfc5d6c7f7623b279125586273bda73d
Transpilation MD5: 44f09c4fa3f14593b6b323e639dbaa49
Author: stefan
Category: javax / gui
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-17 16:36:56
Source code size: 5591 bytes / 192 lines
Pitched / IR pitched: No / No
Views / Downloads: 482 / 1183
Version history: 10 change(s)
Referenced in: #1015563 - SFEditor - Snippet/File Editor
#1015564 - Snippet/File Editor v4 [CURRENT]