Skip to content

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
slavara committed Dec 3, 2020
2 parents 19684d7 + 1f9a57e commit cb2fd99
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package {
public class Main {
public function Main() {
const number:Number = 1;
const intValue:int = 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package {
public class Main {
public function Main() {
var number:Number = 1;
var intValue:int = 1;
}
}
}
12 changes: 6 additions & 6 deletions PostfixCodeCompletion/Completion/Complete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class PCCCompleteFactory
{
public IPCCComplete CreateComplete()
{
switch (PluginBase.CurrentProject)
return PluginBase.CurrentProject switch
{
case AS3Project _: return new PCCASComplete();
case HaxeProject _: return new PCCHaxeComplete();
default: return new PCCComplete();
}
AS3Project _ => new PCCASComplete(),
HaxeProject _ => new PCCHaxeComplete(),
_ => new PCCComplete()
};
}
}

Expand Down Expand Up @@ -215,7 +215,7 @@ void OnHaxeCompletionModeChanged()
if (!(PluginBase.CurrentProject is HaxeProject)) return;
var settings = (HaXeSettings) ((Context) ASContext.GetLanguageContext("haxe")).Settings;
var sdk = settings.InstalledSDKs.FirstOrDefault(it => it.Path == PluginBase.CurrentProject.CurrentSDK);
if (sdk == null || new SemVer(sdk.Version) > "3.2.0") return;
if (sdk == null || new SemVer(sdk.Version) < "3.2.0") return;
switch (settings.CompletionMode)
{
case HaxeCompletionModeEnum.CompletionServer:
Expand Down
21 changes: 8 additions & 13 deletions PostfixCodeCompletion/Completion/HaxeComplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ static void EscapeMacros(IList<string> args)

int GetDisplayPosition()
{
switch (CompilerService)
return CompilerService switch
{
case HaxeCompilerService.Type:
return Expr.Context.Position + 1;
}
return Expr.Context.Position;
HaxeCompilerService.Type => Expr.Context.Position + 1,
_ => Expr.Context.Position
};
}

HaxeCompleteStatus ParseLines(string lines)
Expand All @@ -168,15 +167,11 @@ HaxeCompleteStatus ParseLines(string lines)
Errors = lines.Trim();
return HaxeCompleteStatus.Error;
}
try
try
{
using (TextReader stream = new StringReader(lines))
{
using (var reader = new XmlTextReader(stream))
{
return ProcessResponse(reader);
}
}
using var stream = new StringReader(lines);
using var reader = new XmlTextReader(stream);
return ProcessResponse(reader);
}
catch (Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions PostfixCodeCompletion/Helpers/ScintillaControlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ public string Value
Bitmap icon;
public Bitmap Icon
{
get => icon ?? (icon = (Bitmap)PluginBase.MainForm.FindImage("341"));
get => icon ??= (Bitmap)PluginBase.MainForm.FindImage("341");
set => icon = value;
}

string description;
public string Description => description ?? (description = TemplateUtils.GetDescription(expr, template, Pattern));
public string Description => description ??= TemplateUtils.GetDescription(expr, template, Pattern);

public new string ToString() => Description;

Expand Down
66 changes: 35 additions & 31 deletions PostfixCodeCompletion/Helpers/TemplateUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,39 @@ internal static class TemplateUtils
PatternType
};

internal static bool GetHasTemplates() => GetHasTemplates(PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
internal static bool GetHasTemplates()
=> PluginBase.MainForm.CurrentDocument?.SciControl is { } sci
&& GetHasTemplates(sci.ConfigurationLanguage.ToLower());

internal static bool GetHasTemplates(string language)
{
return GetHasTemplates(PathHelper.SnippetDir, language) || Settings.CustomSnippetDirectories.Any(it => GetHasTemplates(it.Path, language));
}
=> GetHasTemplates(PathHelper.SnippetDir, language)
|| Settings.CustomSnippetDirectories.Any(it => GetHasTemplates(it.Path, language));

static bool GetHasTemplates(string snippetPath, string language)
{
snippetPath = GetTemplatesDir(snippetPath, language);
return Directory.Exists(snippetPath) && Directory.GetFiles(snippetPath, "*.fds").Length > 0;
return Directory.Exists(snippetPath) && Directory.EnumerateFiles(snippetPath, "*.fds").Any();
}

static string GetTemplatesDir(string snippetPath) => GetTemplatesDir(snippetPath, PluginBase.MainForm.CurrentDocument.SciControl.ConfigurationLanguage.ToLower());
static string GetTemplatesDir(string snippetPath)
{
return PluginBase.MainForm.CurrentDocument?.SciControl is { } sci
? GetTemplatesDir(snippetPath, sci.ConfigurationLanguage.ToLower())
: null;
}

static string GetTemplatesDir(string snippetPath, string language) => Path.Combine(Path.Combine(snippetPath, language), PostfixGenerators);
static string GetTemplatesDir(string snippetPath, string language) => Path.Combine(snippetPath, language, PostfixGenerators);

internal static Dictionary<string, string> GetTemplates(string type)
{
var pattern = Templates.Contains(type) ? string.Format(PatternBlock, type) : string.Format(PatternTBlock, type);
var result = new Dictionary<string, string>();
var paths = Settings.CustomSnippetDirectories.Select(it => GetTemplatesDir(it.Path)).ToList();
paths.Add(GetTemplatesDir(PathHelper.SnippetDir));
paths.RemoveAll(s => !Directory.Exists(s));
if (paths.Count == 0) return result;
var pattern = Templates.Contains(type)
? string.Format(PatternBlock, type)
: string.Format(PatternTBlock, type);
foreach (var path in paths)
{
foreach (var file in Directory.GetFiles(path, "*.fds"))
Expand All @@ -83,11 +92,12 @@ internal static string GetTemplate(string snippet, string[] types)
var result = string.Empty;
foreach (var type in types)
{
var r = GetTemplate(snippet, type);
if (!string.IsNullOrEmpty(r) && r != result) result = r;
var template = GetTemplate(snippet, type);
if (!string.IsNullOrEmpty(template) && template != result) result = template;
}
return result;
}

internal static string GetTemplate(string snippet, string type)
{
var marker = $"#pcc:{type}";
Expand All @@ -105,12 +115,9 @@ internal static string GetTemplate(string snippet, string type)

static string GetSnippet(string file)
{
string content;
using (var reader = new StreamReader(File.OpenRead(file)))
{
content = reader.ReadToEnd();
reader.Close();
}
using var reader = new StreamReader(File.OpenRead(file));
var content = reader.ReadToEnd();
reader.Close();
return content;
}

Expand All @@ -127,27 +134,24 @@ internal static KeyValuePair<string, string> GetVarNameToQualifiedName(ASResult
var sci = PluginBase.MainForm.CurrentDocument.SciControl;
var lineNum = sci.CurrentLine;
var word = Reflector.ASGenerator.GetStatementReturnType(sci, sci.GetLine(lineNum), sci.PositionFromLine(lineNum))?.Word;
var varname = string.Empty;
if (member?.Name != null) varname = Reflector.ASGenerator.GuessVarName(member.Name, type);
var value = string.Empty;
if (member?.Name != null) value = Reflector.ASGenerator.GuessVarName(member.Name, type);
if (!string.IsNullOrEmpty(word) && char.IsDigit(word[0])) word = null;
if (!string.IsNullOrEmpty(word) && (string.IsNullOrEmpty(type) || Regex.IsMatch(type, "(<[^]]+>)"))) word = null;
if (!string.IsNullOrEmpty(type) && type == ASContext.Context.Features.voidKey) type = null;
if (string.IsNullOrEmpty(varname)) varname = Reflector.ASGenerator.GuessVarName(word, type);
if (!string.IsNullOrEmpty(varname) && varname == word) varname = $"{varname}1";
return new KeyValuePair<string, string>(varname, type);
if (string.IsNullOrEmpty(value)) value = Reflector.ASGenerator.GuessVarName(word, type);
if (!string.IsNullOrEmpty(value) && value == word) value = $"{value}1";
return new KeyValuePair<string, string>(value, type);
}

internal static string ProcessTemplate(string pattern, string template, ASResult expr)
{
switch (pattern)
return pattern switch
{
case PatternCollection:
return ProcessCollectionTemplate(template, expr);
case PatternHash:
return ProcessHashTemplate(template, expr);
default:
return template;
}
PatternCollection => ProcessCollectionTemplate(template, expr),
PatternHash => ProcessHashTemplate(template, expr),
_ => template
};
}

internal static string ProcessMemberTemplate(string template, ASResult expr)
Expand Down Expand Up @@ -203,7 +207,7 @@ internal static string GetDescription(ASResult expr, string template, string pcc
{
var sci = PluginBase.MainForm.CurrentDocument.SciControl;
var position = ScintillaControlHelper.GetDotLeftStartPosition(sci, sci.CurrentPos - 1);
var exprStartPosition = ASGenerator.GetStartOfStatement(sci, sci.CurrentPos, expr);
var exprStartPosition = ASGenerator.GetStartOfStatement(expr);
var lineNum = sci.CurrentLine;
var line = sci.GetLine(lineNum);
var snippet = line.Substring(exprStartPosition - sci.PositionFromLine(lineNum), position - exprStartPosition);
Expand All @@ -222,7 +226,7 @@ internal static void InsertSnippetText(ASResult expr, string template, string pc
var position = ScintillaControlHelper.GetDotLeftStartPosition(sci, sci.CurrentPos - 1);
sci.SetSel(position, sci.CurrentPos);
sci.ReplaceSel(string.Empty);
position = ASGenerator.GetStartOfStatement(sci, sci.CurrentPos, expr);
position = ASGenerator.GetStartOfStatement(expr);
sci.SetSel(position, sci.CurrentPos);
var snippet = Regex.Replace(template, string.Format(PatternBlock, pccpattern), sci.SelText, RegexOptions.IgnoreCase | RegexOptions.Multiline);
snippet = ProcessMemberTemplate(snippet, expr);
Expand Down
6 changes: 2 additions & 4 deletions PostfixCodeCompletion/PluginMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PluginMain : IPlugin

public int Api => 1;

public string Name => "PostfixCodeCompletion";
public string Name => nameof(PostfixCodeCompletion);

public string Guid => "21d9ab3e-93e4-4460-9298-c62f87eed7ba";

Expand Down Expand Up @@ -97,14 +97,12 @@ void AddEventHandlers()
{
EventManager.AddEventHandler(this, EventType.Command);
EventManager.AddEventHandler(this, EventType.Keys, HandlingPriority.High);
UITools.Manager.OnCharAdded += OnCharAdded;
UITools.Manager.OnCharAdded += (_, value) => Complete.OnCharAdded(value);
}

/// <summary>
/// Saves the plugin settings
/// </summary>
void SaveSettings() => ObjectSerializer.Serialize(settingFilename, Settings);

static void OnCharAdded(ScintillaControl sender, int value) => Complete.OnCharAdded(value);
}
}
10 changes: 5 additions & 5 deletions PostfixCodeCompletion/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ internal class Settings
[Editor(typeof(ArrayEditor), typeof(UITypeEditor))]
public Folder[] CustomSnippetDirectories
{
get { return customSnippetDirectories ?? (customSnippetDirectories = new Folder[] {}); }
set { customSnippetDirectories = value; }
get => customSnippetDirectories ??= Array.Empty<Folder>();
set => customSnippetDirectories = value;
}

bool disableTypeDeclaration = true;

[Category("Haxe"), DisplayName("Disable type declaration for variables"), DefaultValue(true)]
public bool DisableTypeDeclaration
{
get { return disableTypeDeclaration; }
set { disableTypeDeclaration = value; }
get => disableTypeDeclaration;
set => disableTypeDeclaration = value;
}

[Category("Advanced"), DisplayName("Features of languages")]
Expand All @@ -46,6 +46,6 @@ internal class LanguageFeatures
public string Language { get; set; } = string.Empty;

[DisplayName("Numeric types")]
public string[] Numeric { get; set; } = {};
public string[] Numeric { get; set; } = Array.Empty<string>();
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Kind of surround templates on steroids baked with code completion.

## Minimum Requirements
* [Last release FlashDevelop](http://www.flashdevelop.org/community/viewforum.php?f=11).
* [Haxe 3.2.1](http://haxe.org/download/) or higher - for Haxe projects
* [Haxe 3.2.1](http://haxe.org/download/) or higher - for Haxe projects.

### Installation

Expand Down Expand Up @@ -64,9 +64,9 @@ Available templates for **AS3**:
* `$(PCCBoolean)` – логическое
* `$(PCCNullable)` – единица кода, которая может принимать значение null
* `$(PCCCollection)` – коллекция элементов, расположенных в памяти непосредственно друг за другом
* `$(PCCHash)`ассоциативная коллекция
* `$(PCCNumber)`числовое
* `$(PCCString)` – String
* `$(PCCHash)``Object`|`Dictionary`|etc for as3, `Map`|etc - haxe
* `$(PCCNumber)`Numeric
* `$(PCCString)``String`
* http://www.flashdevelop.org/wikidocs/index.php?title=Arguments

Маркеры можно использовать вместе используя разделитель `|`, например сниппет `if($(PCCBoolean|PCCNullable)$(EntryPoint))` будет работать как для логических так и для любых единиц кода, которые могут принимать значени null.
Expand Down

0 comments on commit cb2fd99

Please sign in to comment.