[11700] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11700] | 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.Collections.Generic;
|
---|
[11738] | 23 | using System.Linq;
|
---|
[11700] | 24 | using ICSharpCode.NRefactory.Completion;
|
---|
| 25 | using ICSharpCode.NRefactory.CSharp;
|
---|
| 26 | using ICSharpCode.NRefactory.CSharp.Completion;
|
---|
| 27 | using ICSharpCode.NRefactory.TypeSystem;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.CodeEditor {
|
---|
| 30 | internal class CSharpCodeCompletionStrategy : CodeCompletionStrategy {
|
---|
| 31 | private IProjectContent projectContent = new CSharpProjectContent();
|
---|
| 32 |
|
---|
| 33 | public CSharpCodeCompletionStrategy(CodeEditor codeEditor)
|
---|
| 34 | : base(codeEditor) {
|
---|
| 35 | codeEditor.InternalAssembliesLoaded += (sender, args) => {
|
---|
| 36 | projectContent = projectContent.AddAssemblyReferences(args.Value);
|
---|
| 37 | };
|
---|
| 38 | codeEditor.InternalAssembliesUnloaded += (sender, args) => {
|
---|
| 39 | projectContent = projectContent.RemoveAssemblyReferences(args.Value);
|
---|
| 40 | };
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | protected override CodeCompletionResult GetCodeCompletionResult(bool controlSpace) {
|
---|
| 44 | var document = codeEditor.TextEditor.Document;
|
---|
| 45 | int offset = codeEditor.TextEditor.CaretOffset;
|
---|
| 46 | var result = new CodeCompletionResult();
|
---|
| 47 |
|
---|
[13549] | 48 | try {
|
---|
| 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 | );
|
---|
[11700] | 58 |
|
---|
[13549] | 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 {
|
---|
[11700] | 70 | startPos = completionContext.Offset;
|
---|
[13549] | 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 | }
|
---|
[11700] | 80 | }
|
---|
| 81 |
|
---|
[13549] | 82 | result.TriggerWordLength = triggerWordLength;
|
---|
| 83 | result.TriggerWord = completionContext.Document.GetText(completionContext.Offset - triggerWordLength, triggerWordLength);
|
---|
[11700] | 84 |
|
---|
[13549] | 85 | if (completionData.Any() && cce.AutoCompleteEmptyMatch) {
|
---|
| 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 | }
|
---|
[11738] | 93 | }
|
---|
[11700] | 94 | }
|
---|
| 95 |
|
---|
[13549] | 96 | if (!controlSpace) {
|
---|
| 97 | var pce = new CSharpParameterCompletionEngine(
|
---|
| 98 | completionContext.Document,
|
---|
| 99 | completionContext.CompletionContextProvider,
|
---|
| 100 | completionFactory,
|
---|
| 101 | completionContext.ProjectContent,
|
---|
| 102 | completionContext.TypeResolveContextAtCaret
|
---|
| 103 | );
|
---|
[11700] | 104 |
|
---|
[13549] | 105 | var parameterDataProvider = pce.GetParameterDataProvider(completionContext.Offset, completionChar);
|
---|
| 106 | result.OverloadProvider = parameterDataProvider as IUpdatableOverloadProvider;
|
---|
| 107 | }
|
---|
| 108 | } catch {
|
---|
| 109 | // ignore exceptions thrown during code completion
|
---|
[11700] | 110 | }
|
---|
| 111 |
|
---|
| 112 | return result;
|
---|
| 113 | }
|
---|
[11800] | 114 |
|
---|
| 115 | protected override void DoParseStep() {
|
---|
[12646] | 116 | if (document == null) return;
|
---|
[11816] | 117 | var unresolvedFile = CSharpParsingHelpers.CreateCSharpUnresolvedFile(document);
|
---|
[11800] | 118 | projectContent = projectContent.AddOrUpdateFiles(unresolvedFile);
|
---|
| 119 | }
|
---|
[11700] | 120 | }
|
---|
| 121 | }
|
---|