Skip to content

Commit

Permalink
Merge pull request #7 from ZycaR/master
Browse files Browse the repository at this point in the history
Lexer service
  • Loading branch information
ZycaR committed Oct 6, 2015
2 parents 79efb13 + e8ac37f commit 8418e15
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 8 deletions.
77 changes: 77 additions & 0 deletions Src/SwqlStudio/LexerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SwqlStudio
{
public class LexerService
{
private static LexerService instance;

protected List<string> BasicAutoCompletionKeywords { get; private set; }
protected List<string> CustomAutoCompletionKeywords { get; private set; }
public List<string> AutoCompletionKeywords { get; private set; }

private LexerService()
{
BasicAutoCompletionKeywords = new List<string>();
CustomAutoCompletionKeywords = new List<string>();

foreach (var words in this.LexerKeywords)
BasicAutoCompletionKeywords.AddRange(words.Item2.Select(w => w.ToUpper()));
BasicAutoCompletionKeywords.Sort();

ProcessACKeywords();
}

public static LexerService Instance
{
get
{
if (instance == null)
instance = new LexerService();
return instance;
}
}

public IEnumerable<Tuple<int, IEnumerable<string>>> LexerKeywords
{
get
{
yield return new Tuple<int, IEnumerable<string>>(0,
@"all any and as asc between class desc distinct exists false full group having in inner into is isa from join left like not null or outer right select set some true union where end when then else case on top return xml raw auto with limitation rows to order by desc totalrows noplancache queryplan"
.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries));
// User Keywords 1 - scalar functions
yield return new Tuple<int, IEnumerable<string>>(4,
@"toutc tolocal getdate getutcdate datetime isnull tostring escapeswisurivalue splitstringtoarray floor round ceiling yeardiff monthdiff weekdiff daydiff hourdiff minutediff seconddiff milliseconddiff year quarterofyear dayofyear month week day hour minute second millisecond uriequals arraycontains datetrunc changetimezone toupper tolower concat substring adddate addyear addmonth addweek addday addhour addminute addsecond addmillisecond arraylength arrayvalueat"
.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries));
// User Keywords 2 - aggregate functions
yield return new Tuple<int, IEnumerable<string>>(5,
@"min max avg count sum"
.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries));
}
}

public void AddCustomACKeywords(IEnumerable<string> keywords)
{
this.CustomAutoCompletionKeywords.AddRange(keywords);
ProcessACKeywords();
}

public void ClearCustomACKeywords()
{
this.CustomAutoCompletionKeywords.Clear();
ProcessACKeywords();
}

public void ProcessACKeywords()
{
this.AutoCompletionKeywords = BasicAutoCompletionKeywords
.Concat(CustomAutoCompletionKeywords)
.Distinct(StringComparer.CurrentCultureIgnoreCase)
.OrderBy(w => w)
.ToList();
}
}
}
1 change: 1 addition & 0 deletions Src/SwqlStudio/QueryTab.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions Src/SwqlStudio/SciTextEditorControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Windows.Forms;
using ScintillaNET;
using System.Collections.Generic;

namespace SwqlStudio
{
Expand All @@ -18,14 +19,10 @@ public SciTextEditorControl()

StyleClearAll(); // Propagates the settings from Style.Default to all language-specific lexer styles

SetKeywords(0, @"all any and as asc between class desc distinct exists false full group
having in inner into is isa from join left like not null or outer right
select set some true union where end when then else case on top return xml
raw auto with limitation rows to order by desc totalrows noplancache queryplan");

SetKeywords(4, @"toutc tolocal getdate getutcdate datetime isnull tostring escapeswisurivalue splitstringtoarray floor round ceiling yeardiff monthdiff weekdiff daydiff hourdiff minutediff seconddiff milliseconddiff year quarterofyear dayofyear month week day hour minute second millisecond uriequals arraycontains datetrunc changetimezone toupper tolower concat substring adddate addyear addmonth addweek addday addhour addminute addsecond addmillisecond arraylength arrayvalueat"); // User Keywords 1 - scalar functions

SetKeywords(5, @"min max avg count sum"); // User Keywords 2 - aggregate functions
// TODO: wait for newest nuget package, it will ocntains this method!!!
//this.AutoCSetFillUps(" {}[]().,:;+-*/%&|^!~=<>?@#'\"\\");
foreach (var words in LexerService.Instance.LexerKeywords)
SetKeywords(words.Item1, string.Join(" ", words.Item2));

//// Default (whitespace) style index.
//Styles[Style.Sql.Default].ForeColor = Color.Black;
Expand Down Expand Up @@ -125,5 +122,22 @@ public void SaveFile(string path)
}

public event Action Execute;

protected override void OnCharAdded(CharAddedEventArgs e)
{
base.OnCharAdded(e);

// Find the word start
var currentPos = this.CurrentPosition;
var wordStartPos = this.WordStartPosition(currentPos, true);

var lenEntered = currentPos - wordStartPos;
if (lenEntered <= 0)
return;

// Display the autocompletion list
var keywords = string.Join(" ", LexerService.Instance.AutoCompletionKeywords);
this.AutoCShow(lenEntered, keywords);
}
}
}
1 change: 1 addition & 0 deletions Src/SwqlStudio/SwqlStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<Compile Include="InvokeVerbTab.Designer.cs">
<DependentUpon>InvokeVerbTab.cs</DependentUpon>
</Compile>
<Compile Include="LexerService.cs" />
<Compile Include="LogHeaderMessageInspector.cs" />
<Compile Include="LogHeaderReaderBehavior.cs" />
<Compile Include="Metadata\Entity.cs" />
Expand Down

0 comments on commit 8418e15

Please sign in to comment.