1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using ICSharpCode.NRefactory.Completion;
|
---|
26 | using ICSharpCode.NRefactory.CSharp;
|
---|
27 | using ICSharpCode.NRefactory.CSharp.Completion;
|
---|
28 | using ICSharpCode.NRefactory.TypeSystem;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.CodeEditor {
|
---|
31 | internal class CSharpCodeCompletionStrategy : CodeCompletionStrategy {
|
---|
32 | private IProjectContent projectContent = new CSharpProjectContent();
|
---|
33 |
|
---|
34 | public CSharpCodeCompletionStrategy(CodeEditor codeEditor)
|
---|
35 | : base(codeEditor) {
|
---|
36 | codeEditor.InternalAssembliesLoaded += (sender, args) => {
|
---|
37 | projectContent = projectContent.AddAssemblyReferences(args.Value);
|
---|
38 | };
|
---|
39 | codeEditor.InternalAssembliesUnloaded += (sender, args) => {
|
---|
40 | projectContent = projectContent.RemoveAssemblyReferences(args.Value);
|
---|
41 | };
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected override CodeCompletionResult GetCodeCompletionResult(bool controlSpace) {
|
---|
45 | var document = codeEditor.TextEditor.Document;
|
---|
46 | int offset = codeEditor.TextEditor.CaretOffset;
|
---|
47 |
|
---|
48 | var result = new CodeCompletionResult();
|
---|
49 | var completionContext = new CSharpCodeCompletionContext(document, offset, projectContent);
|
---|
50 | var completionFactory = new CSharpCodeCompletionDataFactory(completionContext);
|
---|
51 | var cce = new CSharpCompletionEngine(
|
---|
52 | completionContext.Document,
|
---|
53 | completionContext.CompletionContextProvider,
|
---|
54 | completionFactory,
|
---|
55 | completionContext.ProjectContent,
|
---|
56 | completionContext.TypeResolveContextAtCaret
|
---|
57 | ) { EolMarker = Environment.NewLine };
|
---|
58 |
|
---|
59 | char completionChar = completionContext.Document.GetCharAt(completionContext.Offset - 1);
|
---|
60 | int startPos, triggerWordLength;
|
---|
61 | IEnumerable<ICompletionData> completionData;
|
---|
62 |
|
---|
63 | if (controlSpace) {
|
---|
64 | if (!cce.TryGetCompletionWord(completionContext.Offset, out startPos, out triggerWordLength)) {
|
---|
65 | startPos = completionContext.Offset;
|
---|
66 | triggerWordLength = 0;
|
---|
67 | }
|
---|
68 | completionData = cce.GetCompletionData(startPos, true);
|
---|
69 | } else {
|
---|
70 | startPos = completionContext.Offset;
|
---|
71 | if (char.IsLetterOrDigit(completionChar) || completionChar == '_') {
|
---|
72 | if (startPos > 1 && char.IsLetterOrDigit(completionContext.Document.GetCharAt((startPos - 2))))
|
---|
73 | return result;
|
---|
74 | completionData = cce.GetCompletionData(startPos, false);
|
---|
75 | triggerWordLength = 1;
|
---|
76 | } else {
|
---|
77 | completionData = cce.GetCompletionData(startPos, false);
|
---|
78 | triggerWordLength = 0;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | result.TriggerWordLength = triggerWordLength;
|
---|
83 | result.TriggerWord = completionContext.Document.GetText(completionContext.Offset - triggerWordLength, triggerWordLength);
|
---|
84 |
|
---|
85 | if (completionData.Any()) {
|
---|
86 | foreach (var completion in completionData) {
|
---|
87 | var cast = completion as CompletionData;
|
---|
88 | if (cast != null) {
|
---|
89 | cast.TriggerWord = result.TriggerWord;
|
---|
90 | cast.TriggerWordLength = result.TriggerWordLength;
|
---|
91 | result.CompletionData.Add(cast);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (!controlSpace) {
|
---|
96 | var pce = new CSharpParameterCompletionEngine(
|
---|
97 | completionContext.Document,
|
---|
98 | completionContext.CompletionContextProvider,
|
---|
99 | completionFactory,
|
---|
100 | completionContext.ProjectContent,
|
---|
101 | completionContext.TypeResolveContextAtCaret
|
---|
102 | );
|
---|
103 |
|
---|
104 | var parameterDataProvider = pce.GetParameterDataProvider(completionContext.Offset, completionChar);
|
---|
105 | result.OverloadProvider = parameterDataProvider as IUpdatableOverloadProvider;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | return result;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|