pascalabcnet/VisualPascalABCNETLinux/IB/CodeCompletion/CodeCompletionProvider.cs
AlexanderZemlyak 0c29ac92b2
Refactoring of Compiler.cs (#2984)
* Add first comments

* Finish commenting for Compile and CompileUnit

* Write TODO sections

* Add a few clarifications

* splitted some functions from compile

* Written some methods from Compile to functions

* Update variable names

* Refactor - stage 1

Refactor GetUsesSection and IsPossibleNameSpace

* Refactor - stage 2

Rename a few functions and variables

* Correct an inaccuracy

* Added comments, look through CompileUnit

* Rename a few functions and add new comments

* Split CompileUnit to Subfunctions

Add IsUnitCompiled, IsUnitInPCU, InitializeNewUnit, GetSourceCode, GenSyntaxTree, GenUnitDocumentation, CheckDLLDirectiveOnlyForLibraries, MatchErrorsToBadNodes, CheckIfUnitModule, SetUseDLLForSystemUnits,
CompileInterfaceDependencies, CompileCurrentUnitInterface, GetImplementationUsesSection, CompileImplementationDependencies, CompileCurrentUnitImplementation

* Added some TODOs

* Return uses_unit_in original name

Renaming of syntax tree nodes leads to internal compiler errors

* Extract GenerateILCode method

* Add checking if recompilation needed method

Needs to be discussed and revised

* Add functions for catch blocks in Compile

* Extract building semantic tree method

Creating main function to be moved to another class

* Rename UnitsSortedList

* Edit CompileUnitsFromDelayedList method

* Change a few variable names, make CreateRCFile function and add comments

* Make code more "user-friendly"

* Add TODOs 31.11.23

* Create PrebuildSemanticTreeActionsMethod

* Refactor semantic checks section in initialize new unit method

* Refactor Adding standard units to uses method

* Return file_name and compiler_directives original names to avoid internal compilation errors

* Refactor GetReferences Method

* Initial refactoring of IncludeNamespaces function

* Create three more methods and wrap important code in regions

* Add TODO's

* Resolve merge conflicts

* Add returned value to ConstructSyntaxTree method

* Fix UnitsSortedList NotFoundError in PCUWriter

* Workaround commit

* Update PABCSystem after tests' changes

* Rename syntax trees in some methods

* Delete CurrentSyntaxUnit variable

* Revert unnecessary project files changes

* Squashed commit of the following:

commit f4d7599f1a39252feac97bd5391f46dfdc25f6f4
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:29 2023 +0300

    added links to identarranger

commit 3bd5d33e2b2969884e61a3279ff9c353013b57fe
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:03 2023 +0300

    Change extension for verybasic to yavb

commit 61294c6e7d405e1c68516cc81d3a109e8a1ee295
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:33:57 2023 +0300

    Change Program Example

commit 86971488341ed6bf13c39e13a513d7ac0c51c285
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:28:59 2023 +0300

    Add IndentArranger to Compile

commit 2ce50bbbf9db22c4243b247b870f6935ce206d26
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 17 10:00:31 2023 +0300

    Add Semicolon After Each Statement

commit 796309d340e8d8ba730ff9418fa376f34fb7fd36
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Wed Nov 15 18:49:11 2023 +0300

    Add Alpha Version of Python-style If-statement

commit 5eb88f946fe8254b4f5c5a56ed0a567b3be51227
Merge: ab4ce5b0 0162b637
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:54:26 2023 +0300

    Merge branch 'IndentArranger' into VeryBasicLanguage

commit ab4ce5b0e32d42004726e3dc45cc0d8a65e53f56
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:53:51 2023 +0300

    Fixes from seminar

commit af74012289d0d08517c13499a2a66cb543fc06d2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 18:57:29 2023 +0300

    finally working!

    fully implemented compatibility

commit 58a39c313324b468d1eec207ba3f5f6eddf74ef2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 11:58:16 2023 +0300

    more compatibility with pabc

    Now verybasic statements translate to pascal compiler
    added .bat script for autobuilding verybasic

commit 0162b6376b8fe970704f26213bf9f0f670dfb12e
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:06:14 2023 +0300

    Update test.txt

commit 1b759ab1cd7ee2ffeb14afff0418ddf0f30b0399
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:05:36 2023 +0300

    Add Indent and Unindent Keywords to Generated File

commit cabda7f3985651cff2a8740f1a5bdea8fe7ac892
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 10 21:25:52 2023 +0300

    Add Generation of Output File

commit 306f033d0176ab559e3622b8505427dbe2e0e203
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:42:18 2023 +0300

    Update IndentArranger.sln

commit 60c0ceced1aa3a7d2e33de0facb1126e295f43b5
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:41:22 2023 +0300

    Add IndentArranger

commit 1f4556c4573dae154fcc167b41ff6ab9e8122136
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Mon Nov 6 22:48:36 2023 +0300

    Made my own VeryBasicParser project

    Copied some code from SaushkinParser
    Tried to compile it and implement into Pascal
    Doesn't work due to grammatik issue

* Squashed commit of the following:

commit 4e73d9ac3ffef68312f06a74dc83afffcf3ccbee
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 15:08:10 2023 +0300

    Fixed GPPG (i think so at least)

commit e5cfc220828b37fb2f35e565683019716ec3f0ce
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:44:27 2023 +0300

    Update Compiler.cs

commit 4698e2e75a5f3b4f523a8bc7733a50e8abb4fca1
Merge: 22aaf2b2 f4d7599f
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:54 2023 +0300

    Merge branch 'IndentArrangerTemp' into VeryBasicLanguage

commit f4d7599f1a39252feac97bd5391f46dfdc25f6f4
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:29 2023 +0300

    added links to identarranger

commit 3bd5d33e2b2969884e61a3279ff9c353013b57fe
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:03 2023 +0300

    Change extension for verybasic to yavb

commit 22aaf2b2508278a9ffce525ffed52419bf72c23f
Merge: c326174f 61294c6e
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:26:36 2023 +0300

    Merge branch 'IndentArrangerTemp' into VeryBasicLanguage

commit 61294c6e7d405e1c68516cc81d3a109e8a1ee295
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:33:57 2023 +0300

    Change Program Example

commit 86971488341ed6bf13c39e13a513d7ac0c51c285
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:28:59 2023 +0300

    Add IndentArranger to Compile

commit c326174ff5ecccf9c4f76d58aea4653f047af049
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 19 15:03:19 2023 +0300

    Added UniversalParserHelper and GPPG

    ShiftReduceParser moved to another project
    UniversalParserHelper project added, most of it copied from SaushkinParser

commit 2ce50bbbf9db22c4243b247b870f6935ce206d26
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 17 10:00:31 2023 +0300

    Add Semicolon After Each Statement

commit 796309d340e8d8ba730ff9418fa376f34fb7fd36
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Wed Nov 15 18:49:11 2023 +0300

    Add Alpha Version of Python-style If-statement

commit 5eb88f946fe8254b4f5c5a56ed0a567b3be51227
Merge: ab4ce5b0 0162b637
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:54:26 2023 +0300

    Merge branch 'IndentArranger' into VeryBasicLanguage

commit ab4ce5b0e32d42004726e3dc45cc0d8a65e53f56
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:53:51 2023 +0300

    Fixes from seminar

commit af74012289d0d08517c13499a2a66cb543fc06d2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 18:57:29 2023 +0300

    finally working!

    fully implemented compatibility

commit 58a39c313324b468d1eec207ba3f5f6eddf74ef2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 11:58:16 2023 +0300

    more compatibility with pabc

    Now verybasic statements translate to pascal compiler
    added .bat script for autobuilding verybasic

commit 0162b6376b8fe970704f26213bf9f0f670dfb12e
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:06:14 2023 +0300

    Update test.txt

commit 1b759ab1cd7ee2ffeb14afff0418ddf0f30b0399
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:05:36 2023 +0300

    Add Indent and Unindent Keywords to Generated File

commit cabda7f3985651cff2a8740f1a5bdea8fe7ac892
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 10 21:25:52 2023 +0300

    Add Generation of Output File

commit 306f033d0176ab559e3622b8505427dbe2e0e203
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:42:18 2023 +0300

    Update IndentArranger.sln

commit 60c0ceced1aa3a7d2e33de0facb1126e295f43b5
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:41:22 2023 +0300

    Add IndentArranger

commit 1f4556c4573dae154fcc167b41ff6ab9e8122136
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Mon Nov 6 22:48:36 2023 +0300

    Made my own VeryBasicParser project

    Copied some code from SaushkinParser
    Tried to compile it and implement into Pascal
    Doesn't work due to grammatik issue

* Changed GPPG project NET Framework version, added .dll to gitignore

* Adding UniversalParserHelper to project, trying to include VeryBasic

* Managed dependencies and got VeryBasicLanguage to work

* Change extension of a test program

* Rebuild changes

What should be added to .gitignore?

* Fix bug related to err0524_res_unit.pas

* Rebuild Parser

* Change Indent and Unindent tokens

* Add Symbol Table to ParserABC.y

* Change Indent Space Number to 2

* Add While Loop

* Add Some Operations to Parser

* Make Initialization Node at the Beginning of a program

* Create Grammar.txt

* Test program added

* Add ELIF and SyntaxHighlight

* Fix TableSymbol

* Add Division

* Add Method Call

* Add For Loop

* Rename SPython Parser Folder

* Add documented comments for CompileUnit method

* Added Errors to SPython

Added Errors.cs
Removed link to PABCSaushkinParser
Minor fixes

* Create default constructor for SourceContext

* Move null check of currentUnit to upper level in CompileUnit

* Move CreateMainFunction method from Compiler to TreeConverter class

* Add gppg and parserhelper to visualpascalabcnet dependencies

* Updated installer files to include GPPG and UniversalParserHelper

* Rename GPPG to ShiftReduceParser

* Fix ShiftReduceParser project dependencies

* Fix ShiftReduceParserDependencies second iteration

* Workaround commit

* Update PABCSystem after tests' changes

* Refactor ConvertDirectives method

* Get rid of legacy standard modules code

* Return varBeginOffset and beginOffset calculation to Compiler class

Размещение метода в SyntaxTreeToSemanticTreeConverter не целесообразно. В комментарии видимо имелось в виду что-то другое.

* Workaround commit

* Update PABCSystem after tests' changes

* Revert SPython changes

Оставляем только изменения связанные с рефакторингом.

* Update .gitignore

Co-authored-by: Sun Serega <sunserega2@gmail.com>

* Resolve a few Sun Serega treds

* Delete comments in ParsersController.cs

* Replace specific path with path variable in Studio.bat

* Add comment in Studio.bat file and return FileName in CompilerError.cs

* Fix TreeSubsidiary.cs encoding and sectCore.nsh indents

* Return old version of TestRunner.exe

* Rename some variables and polish a few methods

* Uncomment accidentally commented code

* Replace spaces with tabs

* Changed dll name from GPPG

* Revert "Changed dll name from GPPG"

This reverts commit c485cc8cb787809b7e9dfa8a361e75f17ed39893.

* Update .gitignore

* Delete Libraries/ShiftReduceParser.dll

* Delete bin\ShiftReduceParser.dll

* Replace tabs with spaces

* Update encoding in Studio.bat

* Fix bug with PABCrtl excluded files

* Refactor StandardModule class

* Add null checks to make debuging easier

* Delete unnecessary null checks in SymTable.cs

---------

Co-authored-by: Владислав Крылов <krylov@sfedu.ru>
Co-authored-by: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Co-authored-by: Sun Serega <sunserega2@gmail.com>
2023-12-18 22:33:27 +03:00

640 lines
31 KiB
C#

// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using ICSharpCode.TextEditor.Document;
using PascalABCCompiler.Parsers;
namespace VisualPascalABC
{
public class CodeCompletionProvider : ICompletionDataProvider
{
public string preSelection;
public static CodeCompletionImagesProvider ImagesProvider;
public static DefaultDispatcher disp;
public bool ByFirstChar;
static CodeCompletionProvider()
{
ImagesProvider = new CodeCompletionImagesProvider();
disp = new DefaultDispatcher();
CodeCompletion.AssemblyDocCache.dispatcher = disp;
CodeCompletion.UnitDocCache.dispatcher = disp;
}
public CodeCompletionProvider()
{
}
public ImageList ImageList
{
get
{
return ImagesProvider.ImageList;
}
}
public string PreSelection
{
get
{
return preSelection;
}
}
public int DefaultIndex
{
get
{
return defaultIndex;
}
}
public UserDefaultCompletionData DefaultCompletionElement
{
get
{
return defaultCompletionElement;
}
}
private UserDefaultCompletionData defaultCompletionElement;
private int defaultIndex = -1;
public CompletionDataProviderKeyResult ProcessKey(char key)
{
if (char.IsLetterOrDigit(key) || key == '_')
{
return CompletionDataProviderKeyResult.NormalKey;
}
else
{
// key triggers insertion of selected items
return CompletionDataProviderKeyResult.InsertionKey;
}
}
/// <summary>
/// Called when entry should be inserted. Forward to the insertion action of the completion data.
/// </summary>
public bool InsertAction(ICompletionData data, TextArea textArea, int insertionOffset, char key)
{
textArea.Caret.Position = textArea.Document.OffsetToPosition(insertionOffset);
if (!(data as UserDefaultCompletionData).IsOnOverrideWindow)
{
int ind = data.Text.IndexOf('<');
if (ind != -1 && data.Text.Length > ind + 1 && data.Text[ind + 1] == '>')
data.Text = data.Text.Substring(0, ind);
}
else
data.Text = data.Description;
return data.InsertAction(textArea, key);
}
public PascalABCCompiler.Parsers.KeywordKind keyword;
public List<PascalABCCompiler.Parsers.Position> GetDefinition(string expr, string fileName, int line, int column, bool only_check)
{
List<PascalABCCompiler.Errors.Error> Errors = new List<PascalABCCompiler.Errors.Error>();
PascalABCCompiler.SyntaxTree.expression e = null;
List<PascalABCCompiler.Parsers.Position> loc = null;
if (VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.compilerLoaded)
e = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.StandartCompiler.ParsersController.GetExpression("test" + System.IO.Path.GetExtension(fileName), expr, Errors, new List<PascalABCCompiler.Errors.CompilerWarning>());
if (e is PascalABCCompiler.SyntaxTree.bin_expr && expr.Contains("<"))
{
expr = expr.Replace("<","&<");
Errors.Clear();
e = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.StandartCompiler.ParsersController.GetExpression("test" + System.IO.Path.GetExtension(fileName), expr, Errors, new List<PascalABCCompiler.Errors.CompilerWarning>());
}
if (e == null)
return loc;
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[fileName];
if (dconv == null)
return loc;
loc = dconv.GetDefinition(e, line, column, keyword, only_check);
return loc;
}
private string construct_header(string meth, CodeCompletion.ProcScope ps, int tabCount)
{
//if (CodeCompletion.CodeCompletionController.currentParser != null)
return CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.ConstructHeader(meth, ps, tabCount);
}
private string construct_header(CodeCompletion.ProcRealization ps, int tabCount)
{
return CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.ConstructHeader(ps, tabCount);
}
public CodeCompletion.SymScope FindScopeByLocation(string fileName, int line, int col)
{
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[fileName];
if (dconv == null) return null;
return dconv.FindScopeByLocation(line, col);
}
public string GetMethodImplementationTextToAdd(string fileName, int line, int col, ref PascalABCCompiler.Parsers.Position pos, TextArea textArea)
{
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[fileName];
if (dconv == null) return null;
CodeCompletion.ProcScope[] procs = dconv.GetNotImplementedMethodHeaders(line, col, ref pos);
if (procs == null || procs.Length == 0) return null;
System.Text.StringBuilder sb = new StringBuilder();
for (int i = 0; i < procs.Length; i++)
{
string meth = construct_header(procs[i] as CodeCompletion.ProcRealization, VisualPABCSingleton.MainForm.UserOptions.CursorTabCount);
sb.Append(meth);
//sb.Append('\n');
}
return sb.ToString();
}
public string GetRealizationTextToAdd(string fileName, int line, int col, ref PascalABCCompiler.Parsers.Position pos, TextArea textArea)
{
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[fileName];
if (dconv == null) return null;
CodeCompletion.ProcScope[] procs = dconv.GetNotImplementedMethods(line, col, ref pos);
if (procs == null) return null;
System.Text.StringBuilder sb = new StringBuilder();
sb.Append("\n\n");
for (int i = 0; i < procs.Length; i++)
{
int off = textArea.Document.PositionToOffset(new TextLocation(procs[i].GetPosition().column - 1, procs[i].GetPosition().line - 1));
string meth = textArea.Document.GetText(off, textArea.Document.PositionToOffset(new TextLocation(procs[i].GetPosition().end_column - 1, procs[i].GetPosition().end_line - 1)) - off + 1);
meth = construct_header(meth, procs[i], VisualPABCSingleton.MainForm.UserOptions.CursorTabCount);
if (i < procs.Length - 1)
{
sb.Append(meth);
sb.Append('\n');
}
else
sb.Append(meth.Trim());
}
return sb.ToString();
}
public List<PascalABCCompiler.Parsers.Position> GetRealization(string expr, string fileName, int line, int column)
{
List<PascalABCCompiler.Errors.Error> Errors = new List<PascalABCCompiler.Errors.Error>();
PascalABCCompiler.SyntaxTree.expression e = null;
List<PascalABCCompiler.Parsers.Position> loc = null;
if (VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.compilerLoaded)
e = VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.StandartCompiler.ParsersController.GetExpression("test" + System.IO.Path.GetExtension(fileName), expr, Errors, new List<PascalABCCompiler.Errors.CompilerWarning>());
if (e == null /*|| Errors.Count > 0*/) return loc;
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[fileName];
if (dconv == null) return loc;
loc = dconv.GetRealization(e, line, column, keyword);
return loc;
}
private RenameForm rf;
public List<SymbolsViewerSymbol> Rename(string expr, string name, string fileName, int line, int column)
{
List<SymbolsViewerSymbol> refers = FindReferences(expr, fileName, line, column, true);
return refers;
}
public List<SymbolsViewerSymbol> Rename(string expr, string name, string fileName, int line, int column, ref string new_val)
{
if (rf == null)
{
rf = new RenameForm();
Form1StringResources.SetTextForAllControls(rf);
}
rf.EditValue = name.Trim(' ').Replace("&", "");
DialogResult dr = rf.ShowDialog();
if (dr == DialogResult.OK)
{
new_val = rf.EditValue;
List<SymbolsViewerSymbol> refers = FindReferences(expr, fileName, line, column, true);
if (refers == null) return null;
return refers;
}
return null;
}
public List<SymbolsViewerSymbol> FindReferences(string expr, string fileName, int line, int column, bool for_refact)
{
List<PascalABCCompiler.Errors.Error> Errors = new List<PascalABCCompiler.Errors.Error>();
PascalABCCompiler.SyntaxTree.expression e = null;
if (VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.compilerLoaded)
e = VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.StandartCompiler.ParsersController.GetExpression("test" + System.IO.Path.GetExtension(fileName), expr, Errors, new List<PascalABCCompiler.Errors.CompilerWarning>());
if (e == null) return new List<SymbolsViewerSymbol>();
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[fileName];
if (dconv == null) return new List<SymbolsViewerSymbol>();
List<SymbolsViewerSymbol> lst = InternalFindReferences(fileName, e, line, column, for_refact);
//if (lst != null && expr != null && for_refact)
//lst.Insert(0,new SymbolsViewerSymbol(new PascalABCCompiler.SourceLocation(file_name,line+1,column,line+1,column+expr.Length),ImagesProvider.IconNumberGotoText));
return lst;
}
private List<SymbolsViewerSymbol> InternalFindReferences(string fname, PascalABCCompiler.SyntaxTree.expression expr, int line, int col, bool for_refact)
{
List<PascalABCCompiler.Parsers.Position> lst = new List<PascalABCCompiler.Parsers.Position>();
List<SymbolsViewerSymbol> svs_lst = new List<SymbolsViewerSymbol>();
try
{
CodeCompletion.DomConverter dc = CodeCompletion.CodeCompletionController.comp_modules[fname] as CodeCompletion.DomConverter;
IBaseScope fnd_scope = null;
IBaseScope cur_sc = null;
if (dc != null)
{
fnd_scope = dc.GetSymDefinition(expr, line, col, keyword);
cur_sc = dc.FindScopeByLocation(line, col);
}
if (fnd_scope != null)
{
foreach (string FileName in CodeCompletionParserController.open_files.Keys)
{
CodeCompletion.CodeCompletionController controller = new CodeCompletion.CodeCompletionController();
string text = VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.SourceFilesProvider(FileName, PascalABCCompiler.SourceFileOperation.GetText) as string;
PascalABCCompiler.SyntaxTree.compilation_unit cu = controller.ParseOnlySyntaxTree(FileName, text);
if (cu != null)
{
dc = CodeCompletion.CodeCompletionController.comp_modules[FileName] as CodeCompletion.DomConverter;
CodeCompletion.ReferenceFinder rf = null;
if (dc != null && dc.visitor.entry_scope != null)
{
rf = new CodeCompletion.ReferenceFinder(fnd_scope, dc.visitor.entry_scope, cu, FileName, for_refact);
lst.AddRange(rf.FindPositions());
}
}
}
PascalABCCompiler.Parsers.Position p = fnd_scope.GetPosition();
bool need_add_def = !for_refact; // true
if (p.file_name != null && need_add_def)
svs_lst.Add(new SymbolsViewerSymbol(new PascalABCCompiler.SourceLocation(p.file_name, p.line, p.column, p.end_line, p.end_column), ImagesProvider.GetPictureNum(fnd_scope.SymbolInfo)));
foreach (PascalABCCompiler.Parsers.Position pos in lst)
{
if (pos.file_name != null)
svs_lst.Add(new SymbolsViewerSymbol(new PascalABCCompiler.SourceLocation(pos.file_name, pos.line, pos.column, pos.end_line, pos.end_column), ImagesProvider.IconNumberGotoText));
}
}
}
catch (Exception e)
{
}
GC.Collect();
return svs_lst;
//return svs_lst.ToArray();
}
public string FindExpression(int off, string Text, int line, int col)
{
if (CodeCompletion.CodeCompletionController.CurrentParser != null)
return CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.FindExpression(off, Text, line, col, out keyword);
return null;
}
private string SkipNew(int off, string Text)
{
int tmp = off;
string expr = null;
while (off >= 0 && Char.IsLetterOrDigit(Text[off])) off--;
while (off >= 0 && (Text[off] == ' ' || char.IsControl(Text[off]))) off--;
if (off >= 1 && Text[off] == '=' && Text[off - 1] == ':')
{
off -= 2;
while (off >= 0 && (Text[off] == ' ' || char.IsControl(Text[off]))) off--;
if (off >= 0 && (Text[off] == '_' || char.IsLetterOrDigit(Text[off]) || Text[off] == ']'))
expr = FindExpression(off + 1, Text, 0, 0);
}
return expr;
}
public ICompletionData[] GetCompletionDataByFirst(int off, string Text, int line, int col, char charTyped, PascalABCCompiler.Parsers.KeywordKind keyw)
{
List<ICompletionData> resultList = new List<ICompletionData>();
List<ICompletionData> lst = new List<ICompletionData>();
try
{
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[FileName];
/*if (dconv == null && CodeCompletion.CodeCompletionNameHelper.system_unit_file_full_name != null
&& (keyw == CodeCompletion.KeywordKind.kw_colon || keyw == CodeCompletion.KeywordKind.kw_of))
{
dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[CodeCompletion.CodeCompletionNameHelper.system_unit_file_full_name];
special_module = true;
}*/
string pattern = charTyped.ToString();
string[] keywords = CodeCompletion.CodeCompletionNameHelper.Helper.GetKeywords();
if (CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.IsTypeAfterKeyword(keyw))
{
keywords = CodeCompletion.CodeCompletionNameHelper.Helper.GetTypeKeywords();
}
if (!CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.IsNamespaceAfterKeyword(keyw))
foreach (string key in keywords)
{
//if (key.StartsWith(pattern, StringComparison.CurrentCultureIgnoreCase))
resultList.Add(new UserDefaultCompletionData(key, null, ImagesProvider.IconNumberKeyword, false));
}
PascalABCCompiler.Parsers.SymInfo[] mis = null;
if (CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.IsNamespaceAfterKeyword(keyw))
{
mis = CodeCompletion.DomConverter.standard_units;
}
if (dconv != null)
{
//if (keyw == PascalABCCompiler.Parsers.KeywordKind.Colon || keyw == PascalABCCompiler.Parsers.KeywordKind.Of || keyw == PascalABCCompiler.Parsers.KeywordKind.TypeDecl)
if (CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.IsTypeAfterKeyword(keyw))
mis = dconv.GetTypeByPattern(pattern, line, col, true, VisualPABCSingleton.MainForm.UserOptions.CodeCompletionNamespaceVisibleRange);
else if (CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.IsNamespaceAfterKeyword(keyw) && mis == null)
mis = dconv.GetNamespaces();
else
mis = dconv.GetNameByPattern(null, line, col, true, VisualPABCSingleton.MainForm.UserOptions.CodeCompletionNamespaceVisibleRange);
}
Hashtable cache = new Hashtable();
if (mis != null)
{
bool stop = false;
ICompletionData data = CompletionDataDispatcher.GetLastUsedItem(charTyped);
foreach (PascalABCCompiler.Parsers.SymInfo mi in mis)
{
if (mi.not_include) continue;
if (cache.Contains(mi.name))
continue;
UserDefaultCompletionData ddd = new UserDefaultCompletionData(mi.name, mi.description, ImagesProvider.GetPictureNum(mi), false);
if (!stop && data != null && string.Compare(mi.name, data.Text, true) == 0)
{
defaultCompletionElement = ddd;
stop = true;
}
else if (!stop && data == null && mi.name.StartsWith(charTyped.ToString(), StringComparison.CurrentCultureIgnoreCase))
{
//defaultCompletionElement = ddd;
lst.Add(ddd);
//stop = true;
}
disp.Add(mi, ddd);
resultList.Add(ddd);
cache[mi.name] = mi;
}
//resultList.Sort();
//defaultCompletionElement = resultList[0] as DefaultCompletionData;
}
}
catch (Exception e)
{
}
lst.Sort();
if (lst.Count > 0) defaultCompletionElement = lst[0] as UserDefaultCompletionData;
ICompletionData[] res_arr = resultList.ToArray();
this.ByFirstChar = true;
return res_arr;
}
public ICompletionData[] GetCompletionData(int off, string Text, int line, int col, char charTyped, PascalABCCompiler.Parsers.KeywordKind keyw)
{
List<ICompletionData> resultList = new List<ICompletionData>();
try
{
string pattern = null;
string expr = null;
bool ctrl_space = charTyped == '\0' || charTyped == '_';
bool shift_space = charTyped == '\0';
bool inside_dot_pattern = false;
bool new_space = keyw == PascalABCCompiler.Parsers.KeywordKind.New;
if (ctrl_space)
{
bool is_pattern = false;
pattern = CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.FindPattern(off, Text, out is_pattern);
if (is_pattern && Text[off - pattern.Length - 1] == '.')
{
inside_dot_pattern = true;
expr = FindExpression(off - pattern.Length - 1, Text, line, col);
}
}
else if (new_space)
{
expr = CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.SkipNew(off - 1, Text, ref keyword);
}
else
if (!new_space && keyw != PascalABCCompiler.Parsers.KeywordKind.Uses)
if (charTyped != '$')
expr = FindExpression(off, Text, line, col);
else
expr = FindExpression(off - 1, Text, line, col);
List<PascalABCCompiler.Errors.Error> Errors = new List<PascalABCCompiler.Errors.Error>();
PascalABCCompiler.SyntaxTree.expression e = null;
if (ctrl_space && !shift_space && (pattern == null || pattern == ""))
{
string[] keywords = CodeCompletion.CodeCompletionNameHelper.Helper.GetKeywords();
foreach (string key in keywords)
{
//if (key.StartsWith(pattern, StringComparison.CurrentCultureIgnoreCase))
resultList.Add(new UserDefaultCompletionData(key, null, ImagesProvider.IconNumberKeyword, false));
}
}
if ((!ctrl_space || inside_dot_pattern) && expr != null)
{
e = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.StandartCompiler.ParsersController.GetTypeAsExpression("test" + System.IO.Path.GetExtension(FileName), expr, Errors, new List<PascalABCCompiler.Errors.CompilerWarning>());
if (e == null)
{
Errors.Clear();
e = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.StandartCompiler.ParsersController.GetExpression("test" + System.IO.Path.GetExtension(FileName), expr, Errors, new List<PascalABCCompiler.Errors.CompilerWarning>());
}
if ((e == null || Errors.Count > 0) && !new_space) return null;
}
SymInfo[] mis = null;
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[FileName];
if (dconv == null)
{
if (keyw == PascalABCCompiler.Parsers.KeywordKind.Uses)
mis = CodeCompletion.DomConverter.standard_units;
else if (!ctrl_space)
return new ICompletionData[0];
}
string fname = FileName;
SymInfo sel_si = null;
string last_used_member = null;
if (dconv != null)
{
if (new_space)
mis = dconv.GetTypes(e, line, col, out sel_si);
else if (keyw == PascalABCCompiler.Parsers.KeywordKind.Uses && mis == null)
{
if (WorkbenchServiceFactory.Workbench.UserOptions.EnableSmartIntellisense)
mis = dconv.GetNamespaces();
else
mis = CodeCompletion.DomConverter.standard_units;
}
else
if (!ctrl_space)
{
CodeCompletion.SymScope dot_sc = null;
mis = dconv.GetName(e, expr, line, col, keyword, ref dot_sc);
if (dot_sc != null && VisualPABCSingleton.MainForm.UserOptions.EnableSmartIntellisense)
{
CompletionDataDispatcher.AddMemberBeforeDot(dot_sc);
last_used_member = CompletionDataDispatcher.GetRecentUsedMember(dot_sc);
}
}
else
{
CodeCompletion.SymScope dot_sc = null;
if (inside_dot_pattern)
{
List<SymInfo> si_list = new List<SymInfo>();
SymInfo[] from_list = dconv.GetName(e, expr, line, col, keyword, ref dot_sc);
for (int i = 0; i < from_list.Length; i++)
{
if (from_list[i].name.StartsWith(pattern, StringComparison.OrdinalIgnoreCase))
si_list.Add(from_list[i]);
}
mis = si_list.ToArray();
}
else
mis = dconv.GetNameByPattern(pattern, line, col, charTyped == '_', VisualPABCSingleton.MainForm.UserOptions.CodeCompletionNamespaceVisibleRange);
}
}
Hashtable cache = null;
if (!CodeCompletion.CodeCompletionController.CurrentParser.CaseSensitive)
cache = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
else
cache = new Hashtable();
int num = 0;
if (mis != null)
{
bool stop = false;
ICompletionData data = null;
foreach (SymInfo mi in mis)
{
if (mi.not_include) continue;
if (cache.Contains(mi.name + mi.kind))
continue;
UserDefaultCompletionData ddd = new UserDefaultCompletionData(mi.addit_name != null ? mi.addit_name : mi.name, mi.description, ImagesProvider.GetPictureNum(mi), false);
disp.Add(mi, ddd);
resultList.Add(ddd);
cache[mi.name + mi.kind] = mi;
/*if (VisualPABCSingleton.MainForm.UserOptions.EnableSmartIntellisense && mi.name != null && mi.name != "" && data == null)
{
data = CompletionDataDispatcher.GetLastUsedItem(mi.name[0]);
if (data != null && data.Text == ddd.Text) data = ddd;
}*/
if (last_used_member != null && last_used_member == mi.name)
{
defaultCompletionElement = ddd;
}
if (sel_si != null && mi == sel_si)
{
defaultCompletionElement = ddd;
stop = true;
}
}
if (defaultCompletionElement == null && data != null)
defaultCompletionElement = data as UserDefaultCompletionData;
}
}
catch (Exception e)
{
}
return resultList.ToArray();
}
private CodeCompletion.CodeCompletionController controller;
private string FileName;
private string Text;
public void CompileInThread()
{
controller.Compile(FileName, Text /*+ ")))));end."*/);
}
public ICompletionData[] ddata;
public ICompletionData[] GenerateCompletionDataWithKeyword(string fileName, TextArea textArea, char charTyped, PascalABCCompiler.Parsers.KeywordKind keyw)
{
controller = new CodeCompletion.CodeCompletionController();
int off = textArea.Caret.Offset;
string text = textArea.Document.TextContent.Substring(0, textArea.Caret.Offset);
//controller.Compile(file_name, text /*+ ")))));end."*/);
FileName = fileName; Text = text;
ICompletionData[] data = GetCompletionData(off, text, textArea.Caret.Line, textArea.Caret.Column, charTyped, keyw);
CodeCompletion.AssemblyDocCache.CompleteDocumentation();
CodeCompletion.UnitDocCache.CompleteDocumentation();
controller = null;
//GC.Collect();
return data;
}
public ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped)
{
return null;
}
public ICompletionData[] GenerateCompletionDataForOverridableMethods(string fileName, TextArea textArea)
{
controller = new CodeCompletion.CodeCompletionController();
List<ICompletionData> lst = new List<ICompletionData>();
int line = textArea.Caret.Line;
int col = textArea.Caret.Column;
try
{
CodeCompletion.DomConverter dconv = (CodeCompletion.DomConverter)CodeCompletion.CodeCompletionController.comp_modules[fileName];
SymInfo[] mis = null;
if (dconv != null)
{
mis = dconv.GetOverridableMethods(line, col);
}
if (mis != null)
{
foreach (SymInfo mi in mis)
{
UserDefaultCompletionData ddd = new UserDefaultCompletionData(mi.name, mi.description, ImagesProvider.GetPictureNum(mi), true);
lst.Add(ddd);
}
}
}
catch (Exception e)
{
}
//lst.Sort();
ICompletionData[] res_arr = lst.ToArray();
controller = null;
return res_arr;
}
public ICompletionData[] GenerateCompletionDataByFirstChar(string fileName, TextArea textArea, char charTyped, PascalABCCompiler.Parsers.KeywordKind keyw)
{
controller = new CodeCompletion.CodeCompletionController();
int off = textArea.Caret.Offset;
string text = textArea.Document.TextContent.Substring(0, textArea.Caret.Offset);
//controller.Compile(file_name, text /*+ ")))));end."*/);
FileName = fileName; Text = text;
ICompletionData[] data = GetCompletionDataByFirst(off, text, textArea.Caret.Line, textArea.Caret.Column, charTyped, keyw);
CodeCompletion.AssemblyDocCache.CompleteDocumentation();
CodeCompletion.UnitDocCache.CompleteDocumentation();
controller = null;
//GC.Collect();
return data;
}
}
}