Removed autosave. Use Java nio API to save files.

This commit is contained in:
Akash Yadav
2022-01-19 19:45:20 +05:30
parent 8ec5ea4e04
commit 8d045f917a
4 changed files with 362 additions and 338 deletions
@@ -649,33 +649,7 @@ public class EditorActivity extends StudioActivity implements FileTreeFragment.F
@Override
public void onTabUnselected (@NonNull TabLayout.Tab tab) {
final var position = mViewModel.getCurrentFileIndex ();
if (position < 0 || position >= mViewModel.getOpenedFileCount ()) {
// This might happen when the file is being closed.
// In this case, the file has already been saved, so we don't need to save it again.
// see #close(int)
return;
}
final var opened = mViewModel.getOpenedFile (position);
if (opened == null) {
LOG.error ("No opened file at index", position);
return;
}
final var frag = getEditorAtIndex (position);
if (frag == null) {
LOG.error ("Cannot save unselected editor. Editor is null.");
return;
}
boolean isGradle = frag.isModified () && opened.getName ().endsWith (".gradle");
frag.save ();
if (isGradle) {
LOG.info ("Gradle files have been modified. Sync needed.");
notifySyncNeeded ();
}
// unimplemented
}
@Override
@@ -920,7 +894,7 @@ public class EditorActivity extends StudioActivity implements FileTreeFragment.F
var opened = mViewModel.getOpenedFile (index);
LOG.info ("Closing file:", opened);
final var editor = getEditorAtIndex (index);
if (editor != null) {
if (editor != null && editor.getEditor () != null) {
editor.save ();
editor.getEditor ().close ();
} else {
@@ -947,7 +921,7 @@ public class EditorActivity extends StudioActivity implements FileTreeFragment.F
// Save and close all files one by one
for (int i = 0; i < count; i++) {
final var editor = getEditorAtIndex (i);
if (editor != null) {
if (editor != null && editor.getEditor () != null) {
editor.save ();
editor.getEditor ().close ();
} else {
@@ -22,6 +22,7 @@ import android.view.LayoutInflater;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.SizeUtils;
@@ -38,7 +39,6 @@ import com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE;
import com.itsaky.androidide.utils.LSPUtils;
import com.itsaky.androidide.utils.Logger;
import com.itsaky.androidide.utils.TypefaceUtils;
import com.itsaky.lsp.models.DiagnosticItem;
import com.itsaky.lsp.models.Range;
import org.antlr.v4.runtime.CharStreams;
@@ -46,10 +46,8 @@ import org.antlr.v4.runtime.Token;
import java.io.File;
import java.io.StringReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import io.github.rosemoe.editor.interfaces.EditorEventListener;
@@ -166,11 +164,32 @@ public class CodeEditorView extends FrameLayout {
}
public boolean save () {
final var text = getText ();
final var saved = FileIOUtils.writeFileFromString (getFile (), text);
notifySaved ();
isModified = false;
return saved;
if (getFile () == null) {
LOG.error ("Cannot save file. File instance is null.");
return false;
}
final var path = getFile ().toPath ();
if (!isModified() && Files.exists (path)) {
LOG.info (getFile ());
LOG.info ("File was not modified. Skipping save operation.");
return false;
}
final var lines = new ArrayList<> (getEditor ().getText ().getLines ());
try {
Files.write (getFile ().toPath (), lines);
notifySaved ();
isModified = false;
return true;
} catch (Throwable e) {
LOG.error ("Failed to save file", getFile (), "\n", e);
return false;
}
}
public Editor getEditor () {
@@ -181,6 +200,7 @@ public class CodeEditorView extends FrameLayout {
return binding;
}
@Nullable
public File getFile () {
return file;
}
@@ -194,7 +214,7 @@ public class CodeEditorView extends FrameLayout {
}
public void onPause () {
CompletableFuture.runAsync (this::save);
// unimplemented
}
public void onResume () {
File diff suppressed because it is too large Load Diff
@@ -37,7 +37,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
@@ -154,7 +153,6 @@ public class BootstrapInstaller {
throw new CompletionException (throwable);
}
}
Os.symlink (symlink.first, symlink.second);
}