2019-07-28 23:53:15 +03:00
using System ;
2019-07-29 00:46:15 +03:00
using System.Collections ;
2019-07-28 23:53:15 +03:00
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
using VisualPascalABC ;
2019-08-02 10:17:49 +03:00
using System.Text.RegularExpressions ;
using ICSharpCode.TextEditor.Document ;
2020-01-03 11:03:46 +03:00
using ICSharpCode.TextEditor ;
2019-07-28 23:53:15 +03:00
namespace CodeTemplatesPlugin
{
public partial class CodeTemplatesForm : WeifenLuo . WinFormsUI . Docking . DockContent
{
public CodeTemplateManager schoolManager ;
2019-07-29 00:46:15 +03:00
public VisualPascalABC . Form1 MainForm ;
2019-07-28 23:53:15 +03:00
2019-08-02 10:17:49 +03:00
private Dictionary < string , string > help = new Dictionary < string , string > ( ) ;
2019-08-02 22:17:42 +03:00
public void LoadTemplates ( )
2019-07-28 23:53:15 +03:00
{
try
{
2019-08-04 12:14:14 +03:00
schoolManager = new CodeTemplateManager ( "school.pct" , true ) ; // искать вначале в локальном каталоге, потом на уровень выше и только потом в системном
2019-07-29 00:46:15 +03:00
listBox1 . Items . Clear ( ) ;
2019-08-02 10:17:49 +03:00
//listBox1.Items.AddRange(schoolManager.ht.Keys.ToArray());
foreach ( var x in schoolManager . ht . Keys )
{
var a = x . Split ( '/' ) ;
listBox1 . Items . Add ( a [ 0 ] ) ;
a [ 1 ] = a [ 1 ] . Trim ( ) ;
help [ a [ 0 ] ] = a [ 2 ] ;
var ind = - 1 ;
2019-08-02 22:17:42 +03:00
for ( var i = 0 ; i < listBox1 . Groups . Count ; i + + )
2019-08-02 10:17:49 +03:00
{
if ( listBox1 . Groups [ i ] . Name = = a [ 1 ] )
{
ind = i ;
break ;
2019-08-02 22:17:42 +03:00
}
2019-08-02 10:17:49 +03:00
}
if ( ind = = - 1 )
{
listBox1 . Groups . Add ( a [ 1 ] , a [ 1 ] ) ;
ind = listBox1 . Groups . Count - 1 ;
}
listBox1 . Items [ listBox1 . Items . Count - 1 ] . Group = listBox1 . Groups [ ind ] ;
}
2019-07-28 23:53:15 +03:00
}
catch
{
}
}
2019-08-02 22:17:42 +03:00
public CodeTemplatesForm ( )
{
InitializeComponent ( ) ;
2020-04-23 00:45:34 +03:00
var scale = VisualPascalABC . ScreenScale . Calc ( ) ;
2019-08-02 22:17:42 +03:00
listBox1 . TileSize = new Size ( listBox1 . TileSize . Width , System . Convert . ToInt32 ( 16 * scale ) ) ;
LoadTemplates ( ) ;
}
2019-07-28 23:53:15 +03:00
private void CodeTemplatesForm_FormClosing ( object sender , FormClosingEventArgs e )
{
Hide ( ) ;
e . Cancel = true ;
}
2019-07-29 00:46:15 +03:00
2019-08-02 10:17:49 +03:00
string GetLine ( int lineNum )
{
var ta = MainForm . CurrentCodeFileDocument . TextEditor . ActiveTextAreaControl . TextArea ;
var doc = ta . Document ;
if ( lineNum < doc . TotalNumberOfLines & & lineNum > = 0 )
{
var cseg = doc . GetLineSegment ( lineNum ) ;
return TextUtilities . GetLineAsString ( doc , lineNum ) ;
}
else return null ;
}
string GetPrevLine ( int lineNum ) // предыдущая непустая строка
{
var ta = MainForm . CurrentCodeFileDocument . TextEditor . ActiveTextAreaControl . TextArea ;
string Prev = null ;
if ( ta . Caret . Line = = 0 )
return null ;
var j = ta . Caret . Line ;
do
{
j - - ;
Prev = GetLine ( j ) ;
} while ( j > 0 & & Prev . Trim ( ) = = "" ) ;
if ( Prev . Trim ( ) = = "" )
return null ;
else return Prev ;
}
string GetNextLine ( int lineNum ) // следущая непустая строка
2019-07-29 00:46:15 +03:00
{
2019-08-02 10:17:49 +03:00
var ta = MainForm . CurrentCodeFileDocument . TextEditor . ActiveTextAreaControl . TextArea ;
string Next ;
var i = ta . Caret . Line + 1 ;
do
{
Next = GetLine ( i ) ;
i + + ;
} while ( Next ! = null & & string . IsNullOrWhiteSpace ( Next ) ) ;
return Next ;
}
private void listBox1_MouseDoubleClick_1 ( object sender , MouseEventArgs e )
{
2020-08-23 14:45:31 +03:00
if ( listBox1 . SelectedItems = = null )
return ;
2019-08-02 10:17:49 +03:00
var s = listBox1 . SelectedItems [ 0 ] . Text ;
2019-07-29 00:46:15 +03:00
ICSharpCode . TextEditor . TextArea ta = MainForm . CurrentCodeFileDocument . TextEditor . ActiveTextAreaControl . TextArea ;
ta . Focus ( ) ;
2019-08-02 10:17:49 +03:00
var full = schoolManager . ht . Keys . FirstOrDefault ( st = > st . StartsWith ( s ) ) ;
//if (schoolManager.ht.Keys.Select(st=>st.Substring(0,s.IndexOf('/'))).Contains(s))
if ( full ! = null )
2019-07-29 00:46:15 +03:00
{
2019-08-02 10:17:49 +03:00
// Если шаблон начинается с begin и предыдущая конструкция с начала строки - управляющий оператор, то сдвинуть курсор, выровняв по предыдущей конструкции
if ( s . StartsWith ( "begin … end" ) )
{
var Prev = GetPrevLine ( ta . Caret . Line ) ;
Match m = null ;
if ( Prev ! = null )
{
m = Regex . Match ( Prev , @"^\s*(loop|for|while|if|else)" , RegexOptions . IgnoreCase ) ;
if ( m . Groups [ 1 ] . Value . Length > 0 )
{
var Curr = GetLine ( ta . Caret . Line ) ;
2020-01-03 11:03:46 +03:00
if ( Curr . Length > m . Groups [ 1 ] . Index )
2019-08-02 10:17:49 +03:00
{
ta . Caret . Column = Curr . Length ;
2020-01-03 11:03:46 +03:00
ta . InsertString ( new string ( ' ' , Curr . Length - m . Groups [ 1 ] . Index ) ) ;
2019-08-02 10:17:49 +03:00
}
ta . Caret . Column = m . Groups [ 1 ] . Index ;
2020-01-03 11:03:46 +03:00
var doc = ta . Document ;
var tl_beg = new TextLocation ( ta . Caret . Column , ta . Caret . Line ) ;
int offset = doc . PositionToOffset ( tl_beg ) ;
if ( Curr . Length > ta . Caret . Column & & Curr . Substring ( ta . Caret . Column ) . TrimEnd ( ) . Length = = 0 )
doc . Remove ( offset , Curr . Length - ta . Caret . Column ) ;
2019-08-02 10:17:49 +03:00
}
}
var Next = GetNextLine ( ta . Caret . Line ) ;
if ( m ! = null & & m . Groups [ 1 ] . Value . ToLower ( ) . Equals ( "if" ) & & Next ! = null & & Next . TrimStart ( ) . ToLower ( ) . StartsWith ( "else" ) )
{
CodeCompletionActionsManager . GenerateTemplate ( s , ta , schoolManager , false , str = > str . Remove ( str . Length - 1 ) ) ; // Удалить ; в begin end перед else
}
else
{
CodeCompletionActionsManager . GenerateTemplate ( s , ta , schoolManager , false ) ;
}
}
else
{
CodeCompletionActionsManager . GenerateTemplate ( s , ta , schoolManager , false ) ;
}
2019-07-29 00:46:15 +03:00
}
else
{
ta . InsertString ( s ) ;
}
ta . Focus ( ) ;
}
2019-08-02 10:17:49 +03:00
private void listBox1_SizeChanged ( object sender , EventArgs e )
{
2019-08-02 22:17:42 +03:00
if ( listBox1 . Size . Width > 4 )
listBox1 . TileSize = new Size ( listBox1 . Size . Width - 4 , listBox1 . TileSize . Height ) ;
2019-08-02 10:17:49 +03:00
}
private void listBox1_SelectedIndexChanged ( object sender , EventArgs e )
{
if ( listBox1 . SelectedItems . Count = = 0 )
return ;
var s = listBox1 . SelectedItems [ 0 ] . Text ;
if ( help . ContainsKey ( s ) )
{
label1 . Text = help [ s ] ;
}
}
2019-07-28 23:53:15 +03:00
}
}