Update values table when files in 'values' directory change

This commit is contained in:
Akash Yadav
2022-01-22 14:00:18 +05:30
parent 07e49399fc
commit bbfde4efde
3 changed files with 70 additions and 1 deletions
@@ -40,6 +40,7 @@ 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.inflater.values.ValuesTableFactory;
import com.itsaky.lsp.models.Range;
import org.antlr.v4.runtime.CharStreams;
@@ -49,6 +50,7 @@ import java.io.File;
import java.io.StringReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import io.github.rosemoe.editor.interfaces.EditorEventListener;
@@ -184,7 +186,6 @@ public class CodeEditorView extends FrameLayout {
try {
Files.write (getFile ().toPath (), lines);
notifySaved ();
isModified = false;
@@ -369,5 +370,9 @@ public class CodeEditorView extends FrameLayout {
private void notifySaved () {
binding.editor.didSave ();
if (Objects.requireNonNull (getFile ()).getName ().endsWith (".xml")) {
ValuesTableFactory.syncWithFile (getFile ());
}
}
}
@@ -142,6 +142,33 @@ public class ValuesTable {
return (BooleanValue) booleans.getOrDefault (name, null);
}
/**
* Reads the contents of the given file and updates this table.
* @param file The file to sync with.
*/
public void syncWithFile (final File file) throws Exception {
final var start = System.currentTimeMillis ();
final var factory = XmlPullParserFactory.newInstance ();
final var parser = factory.newPullParser ();
parser.setInput (new FileInputStream (file), null);
var event = parser.getEventType ();
while (event != XmlPullParser.END_DOCUMENT) {
if (event == XmlPullParser.START_TAG) {
final var tag = parser.getName ();
if ("resources".equals (tag)) {
event = parser.next ();
continue;
}
ValuesTable.readTag (tag, parser, this);
}
event = parser.next ();
}
LOG.debug ("Syncing with file", file.getName (), "took", (System.currentTimeMillis () - start), "ms");
}
@NonNull
@Override
public String toString () {
@@ -22,6 +22,8 @@ import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* A factory class that reads the files in the "values" directory of given "res" directories.
@@ -66,6 +68,41 @@ public class ValuesTableFactory {
LOG.debug (String.format (Locale.getDefault (), "Created value tables for %d resource directories in %d ms", resDirs.length, (System.currentTimeMillis () - start)));
}
/**
* Notify that the given file's contents were changed and
* the corresponding table must be updated with new values.
*
* @param file The file that was changed.
*/
public static void syncWithFile (final File file) {
if (file == null) {
LOG.error ("Cannot update value tables. Given file is null.");
return;
}
final var pattern = Pattern.compile (".*/src/.*/res/values/(\\w|_)+\\.xml");
if (!pattern.matcher (file.getAbsolutePath ()).matches ()) {
// This file is not a values resource file.
return;
}
final var resDir = Objects.requireNonNull (file.getParentFile ())/* values dir */.getParentFile () /* res dir*/;
var table = valueTables.getOrDefault (resDir, null);
if (table == null) {
table = createTable (resDir);
if (table != null) {
valueTables.put (resDir, table);
}
return;
}
try {
table.syncWithFile (file);
} catch (Throwable e) {
LOG.error ("Failed to sync values table", e);
}
}
private static ValuesTable createTable (File res) {
try {
final var values = new File (res, "values");