-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for multiple languages (#49)
* Implement generic textview supporting multi lang This is using an EditorContext that lets you implement psedit for a lang It can do custom syntax highlighting, formatting, error handling etc. * Add JSON support for psedit We support syntax highlight, formatting and errors * Move helper functions for editor to extensions Moved GetLine, SetCol, StringToRunes and ToRunes to Extensions This is to be able to call the methods from EditorContext * Set default language when creating new file * Fixed inconsistent behavior with modified flag + Unsaved * Fix syntax highlighting when scrolling left/right * Aligned syntax highlighting in Statusbar with coloring, depending on top/right cursor placement * Fix so Errors / Syntax Errors dialogs can only be opened by supported contexts * Reset modified flag when editor text is reverted to original text * Add string extension to parse Newtonsoft error msg
- Loading branch information
1 parent
6a0e98e
commit e4b9211
Showing
13 changed files
with
1,426 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation; | ||
using System.Management.Automation.Runspaces; | ||
using Terminal.Gui; | ||
|
||
namespace psedit | ||
{ | ||
public class PowershellAutocomplete : Autocomplete | ||
{ | ||
private readonly Runspace _runspace; | ||
|
||
public PowershellAutocomplete(Runspace runspace) | ||
{ | ||
_runspace = runspace; | ||
} | ||
|
||
private IEnumerable<string> _suggestions; | ||
|
||
public void Force() | ||
{ | ||
var host = (EditorTextView)HostControl; | ||
var offset = 0; | ||
|
||
for (var lineOffset = 0; lineOffset <= host.CurrentRow; lineOffset++) | ||
{ | ||
if (lineOffset == host.CurrentRow) | ||
{ | ||
offset += host.CurrentColumn; | ||
} | ||
else | ||
{ | ||
offset += host.Runes[lineOffset].Count + Environment.NewLine.Length; | ||
} | ||
} | ||
|
||
using (var powerShell = PowerShell.Create()) | ||
{ | ||
powerShell.Runspace = _runspace; | ||
var results = CommandCompletion.CompleteInput(host.Text.ToString(), offset, new Hashtable(), powerShell); | ||
Suggestions = results.CompletionMatches.Select(m => m.CompletionText).ToList().AsReadOnly(); | ||
_suggestions = Suggestions; | ||
} | ||
} | ||
|
||
private void TryGenerateSuggestions() | ||
{ | ||
var host = (EditorTextView)HostControl; | ||
var offset = 0; | ||
|
||
for (var lineOffset = 0; lineOffset <= host.CurrentRow; lineOffset++) | ||
{ | ||
if (lineOffset == host.CurrentRow) | ||
{ | ||
offset += host.CurrentColumn; | ||
} | ||
else | ||
{ | ||
offset += host.Runes[lineOffset].Count + Environment.NewLine.Length; | ||
} | ||
} | ||
var text = host.Text.ToString(); | ||
|
||
if (text.Length == 0 || offset == 0 || host.CurrentColumn == 0) | ||
{ | ||
ClearSuggestions(); | ||
return; | ||
} | ||
|
||
var currentChar = text[offset - 1]; | ||
|
||
if (currentChar != '-' && | ||
currentChar != ':' && | ||
currentChar != '.' && | ||
currentChar != '.' && | ||
currentChar != '\\' && | ||
currentChar != '$') | ||
{ | ||
if (_suggestions != null) | ||
{ | ||
var word = GetCurrentWord(); | ||
if (!System.String.IsNullOrEmpty(word)) | ||
{ | ||
Suggestions = _suggestions.Where(m => m.StartsWith(word, StringComparison.OrdinalIgnoreCase)).ToList().AsReadOnly(); | ||
} | ||
else | ||
{ | ||
ClearSuggestions(); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
using (var powerShell = PowerShell.Create()) | ||
{ | ||
powerShell.Runspace = _runspace; | ||
var results = CommandCompletion.CompleteInput(host.Text.ToString(), offset, new Hashtable(), powerShell); | ||
Suggestions = results.CompletionMatches.Select(m => m.CompletionText).ToList().AsReadOnly(); | ||
_suggestions = Suggestions; | ||
} | ||
} | ||
|
||
public override void GenerateSuggestions() | ||
{ | ||
try | ||
{ | ||
TryGenerateSuggestions(); | ||
} | ||
catch { } | ||
} | ||
|
||
public override bool IsWordChar(Rune rune) | ||
{ | ||
var c = (char)rune; | ||
return Char.IsLetterOrDigit(c) || c == '$' || c == '-'; | ||
} | ||
|
||
///<inheritdoc/> | ||
protected override string GetCurrentWord() | ||
{ | ||
var host = (TextView)HostControl; | ||
var currentLine = host.GetCurrentLine(); | ||
var cursorPosition = Math.Min(host.CurrentColumn, currentLine.Count); | ||
return IdxToWord(currentLine, cursorPosition); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void DeleteTextBackwards() | ||
{ | ||
((TextView)HostControl).DeleteCharLeft(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void InsertText(string accepted) | ||
{ | ||
((TextView)HostControl).InsertText(accepted); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using Terminal.Gui; | ||
|
||
namespace psedit | ||
{ | ||
public abstract class EditorContext | ||
{ | ||
public string _originalText; | ||
public int _lastParseTopRow; | ||
public int _lastParseRightColumn; | ||
public int _tabWidth; | ||
public bool CanFormat = false; | ||
public bool CanAutocomplete = false; | ||
public bool CanRun = false; | ||
public bool CanSyntaxHighlight = false; | ||
public ConcurrentDictionary<Point, string> Errors = new ConcurrentDictionary<Point, string>(); | ||
public ConcurrentDictionary<Point, string> ColumnErrors = new ConcurrentDictionary<Point, string>(); | ||
public Dictionary<Point, Terminal.Gui.Color> pointColorDict = new Dictionary<Point, Terminal.Gui.Color>(); | ||
public abstract void ParseText(int height, int topRow, int left, int right, string text, List<List<Rune>> Runes); | ||
public virtual string Format(string text) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
public virtual string Run(string path) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
public virtual string RunText(string text) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
public virtual void RunCurrentRunspace(string path) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
public virtual void RunTextCurrentRunspace(string text) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Color GetColorByPoint(Point point) | ||
{ | ||
Color returnColor = Color.Green; | ||
if (pointColorDict.ContainsKey(point)) | ||
{ | ||
returnColor = pointColorDict[point]; | ||
} | ||
return returnColor; | ||
} | ||
} | ||
} |
Oops, something went wrong.