pascalabcnet/VisualPascalABCNETLinux/IB/CodeCompletion/CodeCompletionActions.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

725 lines
35 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.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Text;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using ICSharpCode.TextEditor.Gui.InsightWindow;
using PascalABCCompiler;
using PascalABCCompiler.Parsers;
namespace VisualPascalABC
{
public class CodeCompletionActionsManager
{
static CodeCompletionProvider ccp;
public static CodeTemplateManager templateManager;
public static void Rename(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
IDocument doc = textArea.Document;
string textContent = doc.TextContent;
ccp = new CodeCompletionProvider();
string name = null;
string expressionResult = FindOnlyIdent(textContent, textArea, ref name).Trim(' ', '\n', '\t', '\r');
string new_val = null;
List<SymbolsViewerSymbol> refers = ccp.Rename(expressionResult, name, textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column, ref new_val);
if (refers == null || new_val == null) return;
int addit = 0;
PascalABCCompiler.SourceLocation tmp = new PascalABCCompiler.SourceLocation(null, 0, 0, 0, 0);
string file_name = null;
if (CodeCompletion.CodeCompletionNameHelper.Helper.IsKeyword(new_val))
new_val = "&" + new_val;
foreach (SymbolsViewerSymbol svs in refers)
{
if (svs.SourceLocation.BeginPosition.Line != tmp.BeginPosition.Line)
addit = 0;
else if (svs.SourceLocation.BeginPosition.Column < tmp.BeginPosition.Column)
addit = 0;
if (svs.SourceLocation.FileName != file_name)
{
CodeFileDocumentControl cfdoc = VisualPABCSingleton.MainForm.FindTab(svs.SourceLocation.FileName);
if (cfdoc == null) continue;
doc = cfdoc.TextEditor.ActiveTextAreaControl.TextArea.Document;
file_name = svs.SourceLocation.FileName;
}
tmp = svs.SourceLocation;
TextLocation tl_beg = new TextLocation(svs.SourceLocation.BeginPosition.Column - 1 + addit, svs.SourceLocation.BeginPosition.Line - 1);
//TextLocation tl_end = new TextLocation(svs.SourceLocation.EndPosition.Line,svs.SourceLocation.EndPosition.Column);
int offset = doc.PositionToOffset(tl_beg);
addit += new_val.Length - name.Length;
doc.Replace(offset, name.Length, new_val);
doc.CommitUpdate();
}
}
public static void RenameUnit(string FileName, string new_val)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
ccp = new CodeCompletionProvider();
IDocument doc = null;
CodeCompletion.CodeCompletionController controller = new CodeCompletion.CodeCompletionController();
string text = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.SourceFilesProvider(FileName, PascalABCCompiler.SourceFileOperation.GetText) as string;
PascalABCCompiler.SyntaxTree.compilation_unit cu = controller.ParseOnlySyntaxTree(FileName, text);
PascalABCCompiler.SyntaxTree.ident unitName = null;
if (cu is PascalABCCompiler.SyntaxTree.unit_module)
{
unitName = (cu as PascalABCCompiler.SyntaxTree.unit_module).unit_name.idunit_name;
}
else if (cu is PascalABCCompiler.SyntaxTree.program_module)
{
if ((cu as PascalABCCompiler.SyntaxTree.program_module).program_name == null)
return;
unitName = (cu as PascalABCCompiler.SyntaxTree.program_module).program_name.prog_name;
}
if (unitName.source_context == null)
return;
List<SymbolsViewerSymbol> refers = ccp.Rename(unitName.name, unitName.name, FileName, unitName.source_context.begin_position.line_num, unitName.source_context.begin_position.column_num);
if (refers == null || new_val == null) return;
int addit = 0;
PascalABCCompiler.SourceLocation tmp = new PascalABCCompiler.SourceLocation(null, 0, 0, 0, 0);
string file_name = null;
VisualPABCSingleton.MainForm.StopTimer();
WorkbenchServiceFactory.CodeCompletionParserController.StopParseThread();
foreach (IFileInfo fi in ProjectFactory.Instance.CurrentProject.SourceFiles)
{
WorkbenchServiceFactory.CodeCompletionParserController.RegisterFileForParsing(fi.Path);
}
WorkbenchServiceFactory.CodeCompletionParserController.ParseInThread();
foreach (SymbolsViewerSymbol svs in refers)
{
if (svs.SourceLocation.BeginPosition.Line != tmp.BeginPosition.Line)
addit = 0;
else if (svs.SourceLocation.BeginPosition.Column < tmp.BeginPosition.Column)
addit = 0;
if (svs.SourceLocation.FileName != file_name)
{
CodeFileDocumentControl cfdoc = VisualPABCSingleton.MainForm.FindTab(svs.SourceLocation.FileName);
if (cfdoc == null) continue;
doc = cfdoc.TextEditor.ActiveTextAreaControl.TextArea.Document;
file_name = svs.SourceLocation.FileName;
}
tmp = svs.SourceLocation;
TextLocation tl_beg = new TextLocation(svs.SourceLocation.BeginPosition.Column - 1, svs.SourceLocation.BeginPosition.Line - 1);
//TextLocation tl_end = new TextLocation(svs.SourceLocation.EndPosition.Line,svs.SourceLocation.EndPosition.Column);
int offset = doc.PositionToOffset(tl_beg);
//addit += new_val.Length - unitName.name.Length;
doc.Replace(offset, unitName.name.Length, new_val);
doc.CommitUpdate();
}
WorkbenchServiceFactory.CodeCompletionParserController.RunParseThread();
VisualPABCSingleton.MainForm.StartTimer();
}
public static List<Position> GetDefinitionPosition(TextArea textArea, bool only_check)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return new List<Position>();
IDocument doc = textArea.Document;
string textContent = doc.TextContent;
ccp = new CodeCompletionProvider();
string full_expr;
string expressionResult = FindFullExpression(textContent, textArea, out ccp.keyword, out full_expr);
if (expressionResult != null)
expressionResult = expressionResult.Trim();
if (expressionResult != full_expr && full_expr.StartsWith("("))
return new List<Position>();
if (full_expr != null && full_expr.Contains("^"))
full_expr = full_expr.Replace("^","");
List<Position> poses = ccp.GetDefinition(full_expr, textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column, only_check);
if (poses == null || poses.Count == 0)
poses = ccp.GetDefinition(expressionResult, textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column, only_check);
return poses;
}
public static List<Position> GetRealizationPosition(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return new List<Position>();
IDocument doc = textArea.Document;
string textContent = doc.TextContent;
ccp = new CodeCompletionProvider();
string full_expr;
string expressionResult = FindFullExpression(textContent, textArea, out ccp.keyword, out full_expr);
List<Position> poses = ccp.GetRealization(full_expr, textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column);
if (poses == null || poses.Count == 0)
poses = ccp.GetRealization(expressionResult, textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column);
return poses;
}
public static List<SymbolsViewerSymbol> FindReferences(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return new List<SymbolsViewerSymbol>();
ccp = new CodeCompletionProvider();
string full_expr;
string expressionResult = FindFullExpression(textArea.Document.TextContent, textArea, out ccp.keyword, out full_expr);
return ccp.FindReferences(expressionResult, textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column, false);
}
public static void GotoRealization(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
position = GetRealizationPosition(textArea);
if (position == null || position.Count == 0) return;
if (position.Count == 1)
{
Position pos = position[0];
if (pos.file_name != null)
{
WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteSourceLocationAction(
new PascalABCCompiler.SourceLocation(pos.file_name, pos.line, pos.column, pos.line, pos.column), VisualPascalABCPlugins.SourceLocationAction.GotoBeg);
}
}
else
{
List<SymbolsViewerSymbol> svs_lst = new List<SymbolsViewerSymbol>();
foreach (Position pos in position)
{
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), CodeCompletionProvider.ImagesProvider.IconNumberGotoText));
}
VisualPABCSingleton.MainForm.FindSymbolResults.showInThread = false;
VisualPABCSingleton.MainForm.ShowFindResults(svs_lst);
VisualPABCSingleton.MainForm.FindSymbolResults.showInThread = true;
}
}
public static bool CanGoTo(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return false;
List<Position> poses = GetDefinitionPosition(textArea, true);
if (poses == null || poses.Count == 0) return false;
foreach (Position pos in poses)
if (pos.from_metadata || pos.file_name != null && (bool)WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.SourceFilesProvider(pos.file_name, PascalABCCompiler.SourceFileOperation.Exists))
return true;
return false;
//string file_name = GetDefinitionPosition(textArea).file_name;
//return file_name != null && (bool)VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.SourceFilesProvider(file_name, PascalABCCompiler.SourceFileOperation.Exists);
}
public static bool CanGoToRealization(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return false;
List<Position> poses = GetRealizationPosition(textArea);
if (poses == null || poses.Count == 0) return false;
foreach (Position pos in poses)
if (pos.file_name != null && (bool)WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.SourceFilesProvider(pos.file_name, PascalABCCompiler.SourceFileOperation.Exists))
return true;
return false;
// string file_name = GetRealizationPosition(textArea).file_name;
// return file_name != null && (bool)VisualPABCSingleton.MainForm.VisualEnvironmentCompiler.SourceFilesProvider(file_name, PascalABCCompiler.SourceFileOperation.Exists);
}
public static bool CanFindReferences(TextArea textArea)
{
try
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return false;
ccp = new CodeCompletionProvider();
string full_expr;
string expressionResult = FindFullExpression(textArea.Document.TextContent, textArea, out ccp.keyword, out full_expr);
if (expressionResult != null)
{
expressionResult = expressionResult.Trim(' ', '\n', '\t', '\r');
return expressionResult != null && expressionResult != "";
}
return expressionResult != null && expressionResult != "";
}
catch (Exception e)
{
return false;
}
}
public static bool CanGenerateRealization(TextArea textArea)
{
try
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return false;
ccp = new CodeCompletionProvider();
Position pos = new Position();
//string text = "procedure Test(a : integer);\n begin \n x := 1; \n end;";//ccp.GetRealizationTextToAdd(out pos);
string text = ccp.GetRealizationTextToAdd(textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column, ref pos, textArea);
return text != null && pos.file_name != null;
}
catch (Exception e)
{
return false;
}
}
public static void GenerateMethodImplementationHeaders(TextArea textArea)
{
try
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
ccp = new CodeCompletionProvider();
Position pos = new Position();
//string text = "procedure Test(a : integer);\n begin \n x := 1; \n end;";//ccp.GetRealizationTextToAdd(out pos);
string text = ccp.GetMethodImplementationTextToAdd(textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column, ref pos, textArea);
if (!string.IsNullOrEmpty(text) && pos.file_name != null)
{
textArea.Caret.Line = pos.line - 1;
textArea.Caret.Column = 0;
textArea.InsertString(text);
textArea.Caret.Line = pos.line - 1;
textArea.Caret.Column = 0;
}
}
catch (System.Exception e)
{
}
}
private static PABCNETCodeCompletionWindow codeCompletionWindow;
private static TextArea _textArea;
public static void GenerateOverridableMethods(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
ccp = new CodeCompletionProvider();
_textArea = textArea;
int off = textArea.Caret.Offset;
codeCompletionWindow = PABCNETCodeCompletionWindow.ShowOverridableMethodsCompletionWindows
(VisualPABCSingleton.MainForm, textArea.MotherTextEditorControl, textArea.MotherTextEditorControl.FileName, ccp);
CodeCompletionAllNamesAction.comp_windows[textArea] = codeCompletionWindow;
if (codeCompletionWindow != null)
{
// ShowCompletionWindow can return null when the provider returns an empty list
codeCompletionWindow.Closed += new EventHandler(CloseCodeCompletionWindow);
}
}
private static void CloseCodeCompletionWindow(object sender, EventArgs e)
{
if (codeCompletionWindow != null)
{
codeCompletionWindow.Closed -= new EventHandler(CloseCodeCompletionWindow);
CodeCompletionProvider.disp.Reset();
codeCompletionWindow.Dispose();
CodeCompletion.AssemblyDocCache.Reset();
CodeCompletion.UnitDocCache.Reset();
codeCompletionWindow = null;
}
CodeCompletionAllNamesAction.comp_windows[_textArea] = null;
}
public static void GenerateClassOrMethodRealization(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
ccp = new CodeCompletionProvider();
Position pos = new Position();
//string text = "procedure Test(a : integer);\n begin \n x := 1; \n end;";//ccp.GetRealizationTextToAdd(out pos);
string text = ccp.GetRealizationTextToAdd(textArea.MotherTextEditorControl.FileName, textArea.Caret.Line, textArea.Caret.Column, ref pos, textArea);
if (text != null && pos.file_name != null)
{
textArea.Caret.Line = pos.line - 1;
textArea.Caret.Column = pos.column - 1;
textArea.InsertString(text);
textArea.Caret.Line = pos.line + 4 - 1;
textArea.Caret.Column = VisualPABCSingleton.MainForm.UserOptions.CursorTabCount + 1;
}
}
private static void find_cursor_pos(string s, out int line, out int col)
{
line = 0;
col = 0;
for (int i = 0; i < s.Length; i++)
if (s[i] == '\n')
{
line++;
col = 0;
}
else if (s[i] == '|')
{
break;
}
else
{
col++;
}
}
public static void GenerateTemplate(string pattern, TextArea textArea) => GenerateTemplate(pattern, textArea, templateManager);
public static void GenerateTemplate(string pattern, TextArea textArea, CodeTemplateManager templateManager, bool withPatternLength = true, Func<string,string> PostAction = null)
{
try
{
StringBuilder sb = new StringBuilder();
IDocument doc = textArea.Document;
if (textArea.SelectionManager.HasSomethingSelected) // удаление выделенного
{
var isel = textArea.SelectionManager.SelectionCollection[0];
textArea.Caret.Line = isel.StartPosition.Line;
textArea.Caret.Column = isel.StartPosition.Column;
textArea.SelectionManager.RemoveSelectedText();
}
int line = textArea.Caret.Line;
int col = textArea.Caret.Column;
string name = templateManager.GetTemplateHeader(pattern);
if (name == null) return;
string templ = templateManager.GetTemplate(name);
int ind = withPatternLength ? pattern.Length : 0;
int cline;
int ccol;
find_cursor_pos(templ, out cline, out ccol);
sb.Append(templ);
int cur_ind = templ.IndexOf('|');
if (cur_ind != -1)
sb = sb.Remove(cur_ind, 1);
sb = sb.Replace("<filename>", Path.GetFileNameWithoutExtension(textArea.MotherTextEditorControl.FileName));
templ = sb.ToString();
int i = 0;
i = templ.IndexOf('\n', i);
while (i != -1)
{
if (i + 1 < sb.Length)
sb.Insert(i + 1, " ", col - ind);
i = sb.ToString().IndexOf('\n', i + 1);
}
TextLocation tl_beg = new TextLocation(col - ind, line);
int offset = doc.PositionToOffset(tl_beg);
doc.Replace(offset, ind, "");
doc.CommitUpdate();
textArea.Caret.Column = col - ind;
var sbs = sb.ToString();
if (PostAction != null)
{
sbs = PostAction(sbs);
}
textArea.InsertString(sbs);
textArea.Caret.Line = line + cline;
textArea.Caret.Column = col - ind + ccol;
}
catch (Exception e)
{
}
}
private static bool should_insert_comment(TextArea textArea)
{
string content = textArea.Document.TextContent;
LineSegment seg = textArea.Document.GetLineSegment(textArea.Caret.Line);
string line = textArea.Document.GetText(seg);
if (line.Trim(' ', '\t') != "//")
return false;
if (textArea.Caret.Line + 1 >= textArea.Document.TotalNumberOfLines)
return false;
return true;
}
private static string get_next_line(TextArea textArea)
{
if (textArea.Caret.Line + 1 >= textArea.Document.TotalNumberOfLines)
return null;
LineSegment seg = textArea.Document.GetLineSegment(textArea.Caret.Line + 1);
return textArea.Document.GetText(seg).Trim(' ', '\t');
}
private static string get_possible_description(TextArea textArea, out int len)
{
LineSegment seg = textArea.Document.GetLineSegment(textArea.Caret.Line);
len = 0;
string s = textArea.Document.GetText(seg).TrimStart(' ', '\t');
if (s != "///" && s.StartsWith("///"))
{
len = s.Length - 3;
return s.Substring(3).Trim(' ', '\t');
}
return null;
}
public static bool GenerateCommentTemplate(TextArea textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return false;
ccp = new CodeCompletionProvider();
//if (!should_insert_comment(textArea))
// return false;
string lineText = get_next_line(textArea);
string addit = CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.GetDocumentTemplate(
lineText, textArea.Document.TextContent, textArea.Caret.Line, textArea.Caret.Column, textArea.Caret.Offset);
if (addit == null)
return false;
int col = textArea.Caret.Column;
int line = textArea.Caret.Line;
int len;
string desc = get_possible_description(textArea, out len);
StringBuilder sb = new StringBuilder();
sb.AppendLine(" <summary>");
for (int i = 0; i < col - 3; i++)
sb.Append(' ');
sb.Append("/// ");
if (desc != null)
sb.Append(desc);
sb.AppendLine();
for (int i = 0; i < col - 3; i++)
sb.Append(' ');
sb.Append("/// </summary>");
if (addit != "")
{
sb.Append(addit);
}
if (desc == null)
textArea.InsertString(sb.ToString());
else
{
IDocument doc = textArea.Document;
TextLocation tl_beg = new TextLocation(col, line);
//TextLocation tl_end = new TextLocation(svs.SourceLocation.EndPosition.Line,svs.SourceLocation.EndPosition.Column);
int offset = doc.PositionToOffset(tl_beg);
doc.Replace(offset, len, "");
doc.CommitUpdate();
textArea.InsertString(sb.ToString());
}
textArea.Caret.Line = line + 1;
textArea.Caret.Column = col + 1;
return true;
}
public static void GotoDefinition(TextArea _textArea)
{
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
position = GetDefinitionPosition(_textArea, false);
if (position == null) return;
if (position.Count == 1)
{
Position pos = position[0];
if (pos.file_name != null)
{
WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteSourceLocationAction(
new PascalABCCompiler.SourceLocation(pos.file_name, pos.line, pos.column, pos.line, pos.column), VisualPascalABCPlugins.SourceLocationAction.GotoBeg);
}
else
if (pos.from_metadata)
{
WorkbenchServiceFactory.FileService.OpenTabWithText(pos.metadata_title, pos.metadata);
WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteSourceLocationAction(
new PascalABCCompiler.SourceLocation(pos.file_name, pos.line, pos.column, pos.line, pos.column), VisualPascalABCPlugins.SourceLocationAction.GotoBeg);
}
}
else
{
List<SymbolsViewerSymbol> svs_lst = new List<SymbolsViewerSymbol>();
foreach (Position pos in position)
{
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), CodeCompletionProvider.ImagesProvider.IconNumberGotoText));
}
VisualPABCSingleton.MainForm.FindSymbolResults.showInThread = false;
VisualPABCSingleton.MainForm.ShowFindResults(svs_lst);
VisualPABCSingleton.MainForm.FindSymbolResults.showInThread = true;
}
}
private static string FindFullExpression(string Text, TextArea _textArea, out PascalABCCompiler.Parsers.KeywordKind keyw, out string full_expr)
{
keyw = PascalABCCompiler.Parsers.KeywordKind.None;
string expr_without_brackets = null;
full_expr = null;
if (CodeCompletion.CodeCompletionController.CurrentParser != null)
{
full_expr = CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.FindExpressionFromAnyPosition(_textArea.Caret.Offset, Text, _textArea.Caret.Line, _textArea.Caret.Column, out keyw, out expr_without_brackets);
return expr_without_brackets;
}
return null;
}
private static string FindOnlyIdent(string Text, TextArea _textArea, ref string name)
{
return CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.FindOnlyIdentifier(_textArea.Caret.Offset, Text, _textArea.Caret.Line, _textArea.Caret.Column, ref name);
}
private static List<Position> position;
}
public class VirtualMethodsAction : ICSharpCode.TextEditor.Actions.AbstractEditAction
{
public override void Execute(TextArea textArea)
{
CodeCompletionActionsManager.GenerateOverridableMethods(textArea);
}
}
public class ClassOrMethodRealizationAction : ICSharpCode.TextEditor.Actions.AbstractEditAction
{
public override void Execute(TextArea textArea)
{
CodeCompletionActionsManager.GenerateClassOrMethodRealization(textArea);
}
}
public class GenerateMethodImplementationHeadersAction : ICSharpCode.TextEditor.Actions.AbstractEditAction
{
public override void Execute(TextArea textArea)
{
CodeCompletionActionsManager.GenerateMethodImplementationHeaders(textArea);
}
}
public class GotoAction : ICSharpCode.TextEditor.Actions.AbstractEditAction
{
public override void Execute(TextArea textArea)
{
CodeCompletionActionsManager.GotoDefinition(textArea);
}
}
public class CodeCompletionNamesOnlyInModuleAction : CodeCompletionAllNamesAction
{
public override void Execute(TextArea _textArea)
{
key = '\0';
base.Execute(_textArea);
}
}
public class CodeFormattingAction : ICSharpCode.TextEditor.Actions.AbstractEditAction
{
public override void Execute(TextArea textArea)
{
if (WorkbenchServiceFactory.DebuggerManager.IsRunning)
return;
WorkbenchServiceFactory.Workbench.ErrorsListWindow.ClearErrorList();
VisualPABCSingleton.MainForm.CurrentCodeFileDocument.DeselectAll();
CodeFormatters.CodeFormatter cf = new CodeFormatters.CodeFormatter(VisualPABCSingleton.MainForm.UserOptions.TabIndent);
List<PascalABCCompiler.Errors.Error> Errors = new List<PascalABCCompiler.Errors.Error>();
//PascalABCCompiler.SyntaxTree.syntax_tree_node sn =
// MainForm.VisualEnvironmentCompiler.Compiler.ParsersController.Compile(
// file_name, TextEditor.Text, null, Errors, PascalABCCompiler.Parsers.ParseMode.Normal);
string text = WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.SourceFilesProvider(VisualPABCSingleton.MainForm.CurrentCodeFileDocument.FileName, PascalABCCompiler.SourceFileOperation.GetText) as string;
PascalABCCompiler.SyntaxTree.compilation_unit cu =
CodeCompletion.CodeCompletionController.ParsersController.GetCompilationUnitForFormatter(
VisualPABCSingleton.MainForm.CurrentCodeFileDocument.FileName,
text, //VisualPascalABC.Form1.Form1_object._currentCodeFileDocument.TextEditor.Text,
Errors,
new List<PascalABCCompiler.Errors.CompilerWarning>());
if (Errors.Count == 0)
{
string formattedText = cf.FormatTree(text, cu, textArea.Caret.Line + 1, textArea.Caret.Column + 1);
bool success = true;
if (success)
{
//WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteAction(VisualPascalABCPlugins.VisualEnvironmentCompilerAction.SetCurrentSourceFileTextFormatting, "");
WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteAction(VisualPascalABCPlugins.VisualEnvironmentCompilerAction.SetCurrentSourceFileTextFormatting, formattedText);
WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteSourceLocationAction(
new PascalABCCompiler.SourceLocation(VisualPABCSingleton.MainForm.CurrentCodeFileDocument.FileName, cf.Line, cf.Column, cf.Line, cf.Column), VisualPascalABCPlugins.SourceLocationAction.GotoBeg);
}
}
else if (!(Errors[0] is PascalABCCompiler.Errors.ParserBadFileExtension))
{
WorkbenchServiceFactory.Workbench.VisualEnvironmentCompiler.ExecuteAction(VisualPascalABCPlugins.VisualEnvironmentCompilerAction.AddMessageToErrorListWindow, new List<PascalABCCompiler.Errors.Error>(new PascalABCCompiler.Errors.Error[] { Errors[0] }));
}
}
}
public class CodeCompletionAllNamesAction : ICSharpCode.TextEditor.Actions.AbstractEditAction
{
PABCNETCodeCompletionWindow codeCompletionWindow;
public static TextArea textArea;
public static Hashtable comp_windows = new Hashtable();
public static bool is_begin = false;
protected char key = '_';
private string get_prev_text(string text, int off)
{
StringBuilder sb = new StringBuilder();
while (off >= 0 && char.IsLetterOrDigit(text[off]))
{
sb.Insert(0, text[off--]);
}
return sb.ToString();
}
public override void Execute(TextArea _textArea)
{
//try
{
textArea = _textArea;
int off = textArea.Caret.Offset;
string text = textArea.Document.TextContent.Substring(0, textArea.Caret.Offset);
if (key == '\0')
if (off > 2 && text[off - 1] == '/' && text[off - 2] == '/' && text[off - 3] == '/')
{
CodeCompletionActionsManager.GenerateCommentTemplate(textArea);
return;
}
else
{
string prev = get_prev_text(text, off - 1);
if (!string.IsNullOrEmpty(prev))
{
CodeCompletionActionsManager.GenerateTemplate(prev, textArea);
return;
}
}
if (!WorkbenchServiceFactory.Workbench.UserOptions.CodeCompletionDot)
return;
if (CodeCompletion.CodeCompletionController.CurrentParser == null) return;
CodeCompletionProvider completionDataProvider = new CodeCompletionProvider();
bool is_pattern = false;
is_begin = true;
completionDataProvider.preSelection = CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.FindPattern(off, text, out is_pattern);
if (!is_pattern && off > 0 && text[off - 1] == '.')
key = '$';
codeCompletionWindow = PABCNETCodeCompletionWindow.ShowCompletionWindow(
VisualPABCSingleton.MainForm, // The parent window for the completion window
textArea.MotherTextEditorControl, // The text editor to show the window for
textArea.MotherTextEditorControl.FileName, // Filename - will be passed back to the provider
completionDataProvider, // Provider to get the list of possible completions
key, // Key pressed - will be passed to the provider
false,
false,
PascalABCCompiler.Parsers.KeywordKind.None
);
key = '_';
CodeCompletionNamesOnlyInModuleAction.comp_windows[textArea] = codeCompletionWindow;
if (codeCompletionWindow != null)
{
// ShowCompletionWindow can return null when the provider returns an empty list
codeCompletionWindow.Closed += new EventHandler(CloseCodeCompletionWindow);
}
}
//catch (Exception e)
{
}
}
public void CloseCodeCompletionWindow(object sender, EventArgs e)
{
if (codeCompletionWindow != null)
{
codeCompletionWindow.Closed -= new EventHandler(CloseCodeCompletionWindow);
CodeCompletionProvider.disp.Reset();
CodeCompletion.AssemblyDocCache.Reset();
CodeCompletion.UnitDocCache.Reset();
codeCompletionWindow.Dispose();
codeCompletionWindow = null;
}
comp_windows[textArea] = null;
}
}
}