using Newtonsoft.Json; using System; using System.Linq; using System.Collections.Generic; using Windows.Foundation; namespace Monaco.Editor { /// /// Helper to access IModel interface methods off of CodeEditor object. /// https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.imodel.html /// https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.itextmodel.html /// #pragma warning disable CS1591 public sealed class ModelHelper : IModel { private WeakReference _editor; public ModelHelper(CodeEditor editor) { this._editor = new WeakReference(editor); } public string Id => throw new NotImplementedException(); public Uri Uri => throw new NotImplementedException(); public IAsyncAction DetectIndentationAsync(bool defaultInsertSpaces, bool defaultTabSize) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.InvokeScriptAsync("model.detectIndentationAsync", new object[] { defaultInsertSpaces, defaultTabSize }).AsAsyncAction(); } return null; } public IAsyncOperation GetAlternativeVersionIdAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getAlternativeVersionId();").AsAsyncOperation(); } return null; } public IAsyncOperation GetEOLAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getEOL();").AsAsyncOperation(); } return null; } public IAsyncOperation GetFullModelRangeAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getFullModelRange();").AsAsyncOperation(); } return null; } public IAsyncOperation GetLineContentAsync(uint lineNumber) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getLineContent(" + lineNumber + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetLineCountAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getLineCount();").AsAsyncOperation(); } return null; } public IAsyncOperation GetLineFirstNonWhitespaceColumnAsync(uint lineNumber) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getLineFirstNonWhitespaceColumn(" + lineNumber + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetLineLastNonWhitespaceColumnAsync(uint lineNumber) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getLineLastNonWhitespaceColumn(" + lineNumber + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetLineLengthAsync(uint lineNumber) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getLineLength(" + lineNumber + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetLineMaxColumnAsync(uint lineNumber) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getLineMaxColumn(" + lineNumber + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetLineMinColumnAsync(uint lineNumber) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getLineMinColumn(" + lineNumber + ");").AsAsyncOperation(); } return null; } public IAsyncOperation> GetLinesContentAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync>("model.getLinesContent();").AsAsyncOperation(); } return null; } public IAsyncOperation GetModelIdAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getModelId();").AsAsyncOperation(); } return null; } public IAsyncOperation GetOffsetAtAsync(IPosition position) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getOffsetAt(" + JsonConvert.SerializeObject(position) + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetOneIndentAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getOneIndent();").AsAsyncOperation(); } return null; } public IAsyncOperation GetPositionAtAsync(uint offset) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getPositionAt(" + offset + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetValueAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getValue();").AsAsyncOperation(); } return null; } public IAsyncOperation GetValueAsync(EndOfLinePreference eol) { throw new NotImplementedException(); } public IAsyncOperation GetValueAsync(EndOfLinePreference eol, bool preserveBOM) { throw new NotImplementedException(); } public IAsyncOperation GetValueInRangeAsync(IRange range) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getValueInRange(" + JsonConvert.SerializeObject(range) + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetValueInRangeAsync(IRange range, EndOfLinePreference eol) { throw new NotImplementedException(); } public IAsyncOperation GetValueLengthAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getValueLength();").AsAsyncOperation(); } return null; } public IAsyncOperation GetValueLengthAsync(EndOfLinePreference eol) { throw new NotImplementedException(); } public IAsyncOperation GetValueLengthAsync(EndOfLinePreference eol, bool preserveBOM) { throw new NotImplementedException(); } public IAsyncOperation GetValueLengthInRangeAsync(IRange range) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getValueLengthInRange(" + JsonConvert.SerializeObject(range) + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetVersionIdAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getVersionId();").AsAsyncOperation(); } return null; } // TODO: Need to investigate why with .NET Native the InterfaceToClassConverter isn't working anymore? public IAsyncOperation GetWordAtPositionAsync(IPosition position) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getWordAtPosition(" + JsonConvert.SerializeObject(position) + ");").AsAsyncOperation(); } return null; } public IAsyncOperation GetWordUntilPositionAsync(IPosition position) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.getWordUntilPosition(" + JsonConvert.SerializeObject(position) + ");").AsAsyncOperation(); } return null; } public IAsyncOperation ModifyPositionAsync(IPosition position, int number) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.modifyPosition(" + JsonConvert.SerializeObject(position) + ", " + number + ");").AsAsyncOperation(); } return null; } public IAsyncOperation NormalizeIndentationAsync(string str) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.normalizeIndentations(JSON.parse(" + JsonConvert.ToString(str) + "));").AsAsyncOperation(); } return null; } public IAsyncAction PushStackElementAsync() { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.pushStackElement();").AsAsyncAction(); } return null; } public IAsyncAction SetEOLAsync(EndOfLineSequence eol) { throw new NotImplementedException(); } public IAsyncAction SetValue(string newValue) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.setValue(JSON.parse(" + JsonConvert.ToString(newValue) + "));").AsAsyncAction(); } return null; } public IAsyncOperation ValidatePositionAsync(IPosition position) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.validatePosition(" + JsonConvert.SerializeObject(position) + ");").AsAsyncOperation(); } return null; } public IAsyncOperation ValidateRangeAsync(IRange range) { if (_editor.TryGetTarget(out CodeEditor editor)) { return editor.SendScriptAsync("model.validateRange(" + JsonConvert.SerializeObject(range) + ");").AsAsyncOperation(); } return null; } } #pragma warning restore CS1591 }