public void SerializeAndDeserialize_Hash() { SValue hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value"); hash["field1"] = SValue.NewInt(10); Assert.AreEqual("{'field0': 'value', 'field1': 10}", SValue.Unserialize(SValue.Serialize(hash)).ToString()); }
public void SerializeAndDeserialize_Simple() { Assert.AreEqual("'some text'", SValue.Unserialize(SValue.Serialize(SValue.NewString("some text"))).ToString()); Assert.AreEqual("''", SValue.Unserialize(SValue.Serialize(SValue.NewString(""))).ToString()); Assert.AreEqual("''", SValue.Unserialize(SValue.Serialize(SValue.NewString(null))).ToString()); Assert.AreEqual("10", SValue.Unserialize(SValue.Serialize(SValue.NewInt(10))).ToString()); Assert.AreEqual("-150", SValue.Unserialize(SValue.Serialize(SValue.NewInt(-150))).ToString()); Assert.AreEqual("10.1d", SValue.Unserialize(SValue.Serialize(SValue.NewDouble(10.1))).ToString()); Assert.AreEqual("-0.1d", SValue.Unserialize(SValue.Serialize(SValue.NewDouble(-0.1))).ToString()); Assert.AreEqual("True", SValue.Unserialize(SValue.Serialize(SValue.NewBool(true))).ToString()); Assert.AreEqual("False", SValue.Unserialize(SValue.Serialize(SValue.NewBool(false))).ToString()); Assert.AreEqual("1.5f", SValue.Unserialize(SValue.Serialize(SValue.NewFloat(1.5f))).ToString()); Assert.AreEqual("2000L", SValue.Unserialize(SValue.Serialize(SValue.NewLong(2000L))).ToString()); Assert.AreEqual("None", SValue.Unserialize(SValue.Serialize(SValue.None)).ToString()); }
public void AsDictionary() { byte[] bytes; { SValue value = SValue.NewHash(); value["x"] = SValue.NewInt(10); Assert.NotNull(value.AsDictionary, "before serialize"); Assert.AreEqual(1, value.AsDictionary.Count, "before serialize"); Assert.AreEqual(10, value.AsDictionary["x"].Int, "before serialize"); bytes = SValue.Serialize(value); } { SValue value = SValue.Unserialize(bytes); Assert.NotNull(value.AsDictionary, "after serialize"); Assert.AreEqual(1, value.AsDictionary.Count, "after serialize"); Assert.AreEqual(10, value.AsDictionary["x"].Int, "after serialize"); } }
public void SerializeAndDeserialize_CircleLinks() { SValue hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value"); hash["field1"] = SValue.NewInt(10); hash["field2"] = hash; SValue list = SValue.NewList(); list.Add(SValue.NewInt(1)); list.Add(SValue.NewString("value2")); list.Add(hash); hash["field3"] = list; SValue unserialized = SValue.Unserialize(SValue.Serialize(list)); Assert.AreEqual("[1, 'value2', {'field0': 'value', 'field1': 10, 'field2': …, 'field3': …}]", unserialized.ToString()); Assert.AreEqual(unserialized[2], unserialized[2]["field2"]); Assert.AreEqual(unserialized, unserialized[2]["field3"]); Assert.AreNotEqual(unserialized, unserialized[2]["field2"]); }
public void Rescan() { string tempFile = Path.Combine(Path.GetTempPath(), "typewriter-syntax.bin"); SValue temp = File.Exists(tempFile) ? SValue.Unserialize(File.ReadAllBytes(tempFile)) : SValue.None; Dictionary <string, bool> scanned = new Dictionary <string, bool>(); List <string> files = new List <string>(); foreach (string dir in dirs) { if (!Directory.Exists(dir)) { continue; } foreach (string fileI in Directory.GetFiles(dir, "*.xml")) { string fileName = Path.GetFileName(fileI); if (!scanned.ContainsKey(fileName)) { scanned[fileName] = true; files.Add(fileI); } } } scanned.Clear(); SValue newTemp = SValue.NewHash(); infos.Clear(); syntaxFileByName.Clear(); bool changed = false; foreach (string fileI in files) { SValue tempI = temp[fileI]; long newTicks = File.GetLastWriteTime(fileI).Ticks; long ticks = tempI["ticks"].Long; if (newTicks == ticks) { LanguageInfo info = new LanguageInfo(); info.syntax = tempI["syntax"].String; info.patterns = ParsePatterns(tempI["patterns"].String); info.priority = tempI["priority"].Int; syntaxFileByName[info.syntax] = fileI; infos.Add(info); newTemp[fileI] = tempI; } else { changed = true; XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; settings.XmlResolver = null; using (XmlReader reader = XmlReader.Create(fileI, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "language") { string syntax = ""; string patterns = ""; int priority = 0; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); if (reader.Name == "name") { syntax = reader.Value.ToLowerInvariant(); } else if (reader.Name == "extensions") { patterns = reader.Value; } else if (reader.Name == "priority") { int.TryParse(reader.Value, out priority); } } if (!string.IsNullOrEmpty(syntax)) { LanguageInfo info = new LanguageInfo(); info.syntax = syntax; info.patterns = ParsePatterns(patterns); info.priority = priority; syntaxFileByName[info.syntax] = fileI; infos.Add(info); SValue newTempI = SValue.NewHash(); newTempI["syntax"] = SValue.NewString(info.syntax); newTempI["patterns"] = SValue.NewString(patterns); newTempI["priority"] = SValue.NewInt(priority); newTempI["ticks"] = SValue.NewLong(newTicks); newTemp[fileI] = newTempI; } break; } } } } } if (changed) { File.WriteAllBytes(tempFile, SValue.Serialize(newTemp)); } }
public void SerializeAndDeserialize_EqualTextsCovering() { SValue list = SValue.NewList(); SValue hash; hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value1"); hash["field1"] = SValue.NewInt(10); list.Add(hash); hash = SValue.NewHash(); hash["field3"] = SValue.NewString("value2"); hash["field1"] = SValue.NewInt(100); list.Add(hash); Assert.AreEqual("[{'field0': 'value1', 'field1': 10}, {'field3': 'value2', 'field1': 100}]", SValue.Unserialize(SValue.Serialize(list)).ToString()); }
public void SerializeAndDeserialize_List() { SValue hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value"); hash["field1"] = SValue.NewInt(10); SValue list = SValue.NewList(); list.Add(SValue.NewInt(1)); list.Add(SValue.NewString("value2")); list.Add(hash); Assert.AreEqual("[1, 'value2', {'field0': 'value', 'field1': 10}]", SValue.Unserialize(SValue.Serialize(list)).ToString()); }
public void Save(string postfix, bool rememberOpenedFiles) { SValue state = SValue.NewHash(); if (mainForm.WindowState == FormWindowState.Maximized) { state["width"] = SValue.NewInt(mainForm.RestoreBounds.Width); state["height"] = SValue.NewInt(mainForm.RestoreBounds.Height); state["x"] = SValue.NewInt(mainForm.RestoreBounds.X); state["y"] = SValue.NewInt(mainForm.RestoreBounds.Y); } else { state["width"] = SValue.NewInt(mainForm.Width); state["height"] = SValue.NewInt(mainForm.Height); state["x"] = SValue.NewInt(mainForm.Location.X); state["y"] = SValue.NewInt(mainForm.Location.Y); } state["maximized"] = SValue.NewBool(mainForm.WindowState == FormWindowState.Maximized); if (rememberOpenedFiles) { { SValue openedTabs = state.SetNewList("openedTabs"); foreach (Buffer buffer in mainForm.MainNest.buffers.list) { SValue valueI = SValue.NewHash().With("fullPath", SValue.NewString(buffer.FullPath)); openedTabs.Add(valueI); if (buffer == mainForm.MainNest.buffers.list.Selected) { state["selectedTab"] = valueI; } } } if (mainForm.MainNest2 != null) { SValue openedTabs = state.SetNewList("openedTabs2"); foreach (Buffer buffer in mainForm.MainNest2.buffers.list) { SValue valueI = SValue.NewHash().With("fullPath", SValue.NewString(buffer.FullPath)); openedTabs.Add(valueI); if (buffer == mainForm.MainNest2.buffers.list.Selected) { state["selectedTab2"] = valueI; } } } } state["storage"] = storage.Serialize(); state["recently"] = recently.Serialize(); state["recentlyDirs"] = recentlyDirs.Serialize(); state["bm"] = EncodeGlobalBookmakrs(); ValuesSerialize(state); state["commandHistory"] = commandHistory.Serialize(); state["findHistory"] = findHistory.Serialize(); state["findInFilesHistory"] = findInFilesHistory.Serialize(); state["findInFilesTempFilter"] = findInFilesTempFilter.Serialize(); state["findInFilesTempCurrentFilter"] = SValue.NewString(findInFilesTempCurrentFilter.value); state["moveHistory"] = moveHistory.Serialize(); state["replacePatternHistory"] = replacePatternHistory.Serialize(); state["replaceHistory"] = replaceHistory.Serialize(); state["goToLineHistory"] = goToLineHistory.Serialize(); state["findParams"] = findParams.Serialize(); if (!string.IsNullOrEmpty(NullableCurrentDir)) { state["currentDir"] = SValue.NewString(NullableCurrentDir); } state["showFileTree"] = SValue.NewBool(mainForm.FileTreeOpened); state["fileTreeExpanded"] = mainForm.FileTree.GetExpandedTemp(); state["helpPosition"] = SValue.NewInt(helpPosition); state["viHelpPosition"] = SValue.NewInt(viHelpPosition); SerializeSettings(ref state); File.WriteAllBytes(GetTempSettingsPath(postfix, AppPath.StartupDir), SValue.Serialize(state)); }
private void Rescan() { infos.Clear(); snippetFiles.Clear(); string tempFile = Path.Combine(Path.GetTempPath(), "typewriter-snippet.bin"); SValue temp = File.Exists(tempFile) ? SValue.Unserialize(File.ReadAllBytes(tempFile)) : SValue.None; Dictionary <string, bool> scanned = new Dictionary <string, bool>(); List <string> files = new List <string>(); foreach (string dir in dirs) { if (!Directory.Exists(dir)) { continue; } foreach (string fileI in Directory.GetFiles(dir, "*.snippets")) { string fileName = Path.GetFileName(fileI); if (!scanned.ContainsKey(fileName)) { scanned[fileName] = true; files.Add(Path.Combine(dir, fileI)); } } } files.Sort(); scanned.Clear(); SValue newTemp = SValue.NewHash(); infos.Clear(); foreach (string pathI in files) { string fileName = Path.GetFileName(pathI); SValue tempI = temp[fileName]; long newTicks = File.GetLastWriteTime(pathI).Ticks; long ticks = tempI["ticks"].Long; if (newTicks == ticks) { SnippetInfo info = new SnippetInfo(); info.path = pathI; info.patterns = ParseExtenstions(tempI["patterns"].String); infos.Add(info); newTemp[fileName] = tempI; } else { string line = null; using (StreamReader reader = new StreamReader(pathI)) { line = reader.ReadLine(); } string patterns; if (line != null && line.StartsWith("extensions:")) { patterns = line.Substring("extensions:".Length).Trim(); } else { patterns = ""; } SValue newTempI = SValue.NewHash(); newTempI["patterns"] = SValue.NewString(patterns); newTempI["ticks"] = SValue.NewLong(newTicks); newTemp[fileName] = newTempI; SnippetInfo info = new SnippetInfo(); info.path = pathI; info.patterns = ParseExtenstions(patterns); infos.Add(info); } } File.WriteAllBytes(tempFile, SValue.Serialize(newTemp)); }