Exemplo n.º 1
0
		void CollectMembers(string code, string memberName, bool includeOverloads = true)
		{
			StringBuilder sb = new StringBuilder();
			List<int> offsets = new List<int>();
			foreach (var ch in code) {
				if (ch == '$') {
					offsets.Add(sb.Length);
					continue;
				}
				sb.Append(ch);
			}
			var syntaxTree = SyntaxTree.Parse(sb.ToString (), "test.cs");
			var unresolvedFile = syntaxTree.ToTypeSystem();
			var compilation = TypeSystemHelper.CreateCompilation(unresolvedFile);

			var symbol = FindReferencesTest.GetSymbol(compilation, memberName);
			var col = new SymbolCollector();
			col.IncludeOverloads = includeOverloads;
			col.GroupForRenaming = true;

			var result = col.GetRelatedSymbols (new Lazy<TypeGraph>(() => new TypeGraph (compilation.Assemblies)),
			                                   symbol);
			if (offsets.Count != result.Count()) {
				foreach (var a in result)
					Console.WriteLine(a);
			}
			Assert.AreEqual(offsets.Count, result.Count());
			var doc = new ReadOnlyDocument(sb.ToString ());
			result
				.Select(r => doc.GetOffset ((r as IEntity).Region.Begin))
				.SequenceEqual(offsets);
		}
Exemplo n.º 2
0
		public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested,
		                              IProject parentProject, CancellationToken cancellationToken)
		{
			var csharpProject = parentProject as CSharpProject;
			
			CSharpParser parser = new CSharpParser(csharpProject != null ? csharpProject.CompilerSettings : null);
			
			SyntaxTree cu = parser.Parse(fileContent, fileName);
			cu.Freeze();
			
			CSharpUnresolvedFile file = cu.ToTypeSystem();
			ParseInformation parseInfo;
			
			if (fullParseInformationRequested)
				parseInfo = new CSharpFullParseInformation(file, fileContent.Version, cu);
			else
				parseInfo = new ParseInformation(file, fileContent.Version, fullParseInformationRequested);
			
			IDocument document = fileContent as IDocument;
			AddCommentTags(cu, parseInfo.TagComments, fileContent, parseInfo.FileName, ref document);
			if (fullParseInformationRequested) {
				if (document == null)
					document = new ReadOnlyDocument(fileContent, parseInfo.FileName);
				((CSharpFullParseInformation)parseInfo).newFoldings = CreateNewFoldings(cu, document);
			}
			
			return parseInfo;
		}
Exemplo n.º 3
0
			public ReadOnlyDocumentLine(ReadOnlyDocument doc, int lineNumber)
			{
				this.doc = doc;
				this.lineNumber = lineNumber;
				this.offset = doc.GetStartOffset(lineNumber);
				this.endOffset = doc.GetEndOffset(lineNumber);
			}
Exemplo n.º 4
0
        public IEnumerable<ICompletionData> CreateProvider(AutoCompleteRequest request)
        {
            var editorText = request.Buffer ?? "";
            var filename = request.FileName;
            var partialWord = request.WordToComplete ?? "";

            var doc = new ReadOnlyDocument(editorText);
            var loc = new TextLocation(request.Line, request.Column - partialWord.Length);
            int cursorPosition = doc.GetOffset(loc);
            //Ensure cursorPosition only equals 0 when editorText is empty, so line 1,column 1
            //completion will work correctly.
            cursorPosition = Math.Max(cursorPosition, 1);
            cursorPosition = Math.Min(cursorPosition, editorText.Length);

            var res = _parser.ParsedContent(editorText, filename);
            var rctx = res.UnresolvedFile.GetTypeResolveContext(res.Compilation, loc);

            ICompletionContextProvider contextProvider = new DefaultCompletionContextProvider(doc, res.UnresolvedFile);
            var engine = new CSharpCompletionEngine(doc, contextProvider, new CompletionDataFactory(partialWord), res.ProjectContent, rctx)
                {
                    EolMarker = Environment.NewLine
                };

            _logger.Debug("Getting Completion Data");

            IEnumerable<ICompletionData> data = engine.GetCompletionData(cursorPosition, true);
            _logger.Debug("Got Completion Data");

            return data.Where(d => d != null && d.DisplayText.IsValidCompletionFor(partialWord))
                       .FlattenOverloads()
                       .RemoveDupes()
                       .OrderBy(d => d.DisplayText);
        }
Exemplo n.º 5
0
		public void SimpleDocument()
		{
			string text = "Hello\nWorld!\r\n";
			IDocument document = new ReadOnlyDocument(text);
			Assert.AreEqual(text, document.Text);
			Assert.AreEqual(3, document.LineCount);
			
			Assert.AreEqual(0, document.GetLineByNumber(1).Offset);
			Assert.AreEqual(5, document.GetLineByNumber(1).EndOffset);
			Assert.AreEqual(5, document.GetLineByNumber(1).Length);
			Assert.AreEqual(6, document.GetLineByNumber(1).TotalLength);
			Assert.AreEqual(1, document.GetLineByNumber(1).DelimiterLength);
			Assert.AreEqual(1, document.GetLineByNumber(1).LineNumber);
			
			Assert.AreEqual(6, document.GetLineByNumber(2).Offset);
			Assert.AreEqual(12, document.GetLineByNumber(2).EndOffset);
			Assert.AreEqual(6, document.GetLineByNumber(2).Length);
			Assert.AreEqual(8, document.GetLineByNumber(2).TotalLength);
			Assert.AreEqual(2, document.GetLineByNumber(2).DelimiterLength);
			Assert.AreEqual(2, document.GetLineByNumber(2).LineNumber);

			Assert.AreEqual(14, document.GetLineByNumber(3).Offset);
			Assert.AreEqual(14, document.GetLineByNumber(3).EndOffset);
			Assert.AreEqual(0, document.GetLineByNumber(3).Length);
			Assert.AreEqual(0, document.GetLineByNumber(3).TotalLength);
			Assert.AreEqual(0, document.GetLineByNumber(3).DelimiterLength);
			Assert.AreEqual(3, document.GetLineByNumber(3).LineNumber);
		}
        public static IEnumerable<ICompletionData> DoCodeComplete(string editorText, int offset) // not the best way to put in the whole string every time
        {
            var doc = new ReadOnlyDocument(editorText);
            var location = doc.GetLocation(offset);

            string parsedText = editorText; // TODO: Why are there different values in test cases?


            var syntaxTree = new CSharpParser().Parse(parsedText, "program.cs");
            syntaxTree.Freeze();
            var unresolvedFile = syntaxTree.ToTypeSystem();

            var mb = new DefaultCompletionContextProvider(doc, unresolvedFile);

            IProjectContent pctx = new CSharpProjectContent();
            var refs = new List<IUnresolvedAssembly> { mscorlib.Value, systemCore.Value, systemAssembly.Value };
            pctx = pctx.AddAssemblyReferences(refs);
            pctx = pctx.AddOrUpdateFiles(unresolvedFile);

            var cmp = pctx.CreateCompilation();

            var resolver3 = unresolvedFile.GetResolver(cmp, location);
            var engine = new CSharpCompletionEngine(doc, mb, new TestCompletionDataFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext);


            engine.EolMarker = Environment.NewLine;
            engine.FormattingPolicy = FormattingOptionsFactory.CreateMono();

            var data = engine.GetCompletionData(offset, controlSpace: false);
            return data;

        }
Exemplo n.º 7
0
		public static void RandomTests(string filePath, int count, CSharpFormattingOptions policy = null, TextEditorOptions options = null)
		{
			if (File.Exists(filePath))
			{
				var code = File.ReadAllText(filePath);
				var document = new ReadOnlyDocument(code);
				policy = policy ?? FormattingOptionsFactory.CreateMono();
				options = options ?? new TextEditorOptions { IndentBlankLines = false };

				var engine = new CacheIndentEngine(new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true });
				Random rnd = new Random();

				for (int i = 0; i < count; i++) {
					int offset = rnd.Next(document.TextLength);
					engine.Update(offset);
					if (engine.CurrentIndent.Length == 0)
						continue;
				}

			}
			else
			{
				Assert.Fail("File " + filePath + " doesn't exist.");
			}
		}
		public static CacheIndentEngine CreateEngine(string text, CSharpFormattingOptions formatOptions = null, TextEditorOptions options = null)
		{
			if (formatOptions == null) {
				formatOptions = FormattingOptionsFactory.CreateMono();
				formatOptions.AlignToFirstIndexerArgument = formatOptions.AlignToFirstMethodCallArgument = true;
			}
			
			var sb = new StringBuilder();
			int offset = 0;
			for (int i = 0; i < text.Length; i++) {
				var ch = text [i];
				if (ch == '$') {
					offset = i;
					continue;
				}
				sb.Append(ch);
			}
			
			var document = new ReadOnlyDocument(sb.ToString());
			options = options ?? new TextEditorOptions { EolMarker = "\n" };
			
			var result = new CacheIndentEngine(new CSharpIndentEngine(document, options, formatOptions));
			result.Update(offset);
			return result;
		}
Exemplo n.º 9
0
        public void SimpleDocument()
        {
            string    text     = "Hello\nWorld!\r\n";
            IDocument document = new ReadOnlyDocument(text);

            Assert.AreEqual(text, document.Text);
            Assert.AreEqual(3, document.LineCount);

            Assert.AreEqual(0, document.GetLineByNumber(1).Offset);
            Assert.AreEqual(5, document.GetLineByNumber(1).EndOffset);
            Assert.AreEqual(5, document.GetLineByNumber(1).Length);
            Assert.AreEqual(6, document.GetLineByNumber(1).TotalLength);
            Assert.AreEqual(1, document.GetLineByNumber(1).DelimiterLength);
            Assert.AreEqual(1, document.GetLineByNumber(1).LineNumber);

            Assert.AreEqual(6, document.GetLineByNumber(2).Offset);
            Assert.AreEqual(12, document.GetLineByNumber(2).EndOffset);
            Assert.AreEqual(6, document.GetLineByNumber(2).Length);
            Assert.AreEqual(8, document.GetLineByNumber(2).TotalLength);
            Assert.AreEqual(2, document.GetLineByNumber(2).DelimiterLength);
            Assert.AreEqual(2, document.GetLineByNumber(2).LineNumber);

            Assert.AreEqual(14, document.GetLineByNumber(3).Offset);
            Assert.AreEqual(14, document.GetLineByNumber(3).EndOffset);
            Assert.AreEqual(0, document.GetLineByNumber(3).Length);
            Assert.AreEqual(0, document.GetLineByNumber(3).TotalLength);
            Assert.AreEqual(0, document.GetLineByNumber(3).DelimiterLength);
            Assert.AreEqual(3, document.GetLineByNumber(3).LineNumber);
        }
 public ReadOnlyDocumentLine(ReadOnlyDocument doc, int lineNumber)
 {
     this.doc        = doc;
     this.lineNumber = lineNumber;
     this.offset     = doc.GetStartOffset(lineNumber);
     this.endOffset  = doc.GetEndOffset(lineNumber);
 }
 /// <inheritdoc/>
 public IDocument CreateDocumentSnapshot()
 {
     if (documentSnapshot == null)
     {
         documentSnapshot = new ReadOnlyDocument(this);
     }
     return(documentSnapshot);
 }
Exemplo n.º 12
0
		/// <summary>
		/// Converts a readonly TextDocument to a Block and applies the provided highlighting definition.
		/// </summary>
		public static Block ConvertTextDocumentToBlock(ReadOnlyDocument document, IHighlightingDefinition highlightingDefinition)
		{
			IHighlighter highlighter;
			if (highlightingDefinition != null)
				highlighter = new DocumentHighlighter(document, highlightingDefinition);
			else
				highlighter = null;
			return ConvertTextDocumentToBlock(document, highlighter);
		}
Exemplo n.º 13
0
        protected static IDocument GetResult(CSharpFormattingOptions policy, string input)
        {
            var adapter = new ReadOnlyDocument (input);
            var visitor = new AstFormattingVisitor (policy, adapter, factory);

            var compilationUnit = new CSharpParser ().Parse (new StringReader (input), "test.cs");
            compilationUnit.AcceptVisitor (visitor, null);

            return new ReadOnlyDocument (ApplyChanges (input, visitor.Changes));
        }
		void AssertOutput(AstNode node)
		{
			RemoveTokens(node);
			StringWriter w = new StringWriter();
			w.NewLine = "\n";
			node.AcceptVisitor(new CSharpOutputVisitor(TokenWriter.CreateWriterThatSetsLocationsInAST(w), FormattingOptionsFactory.CreateSharpDevelop()));
			var doc = new ReadOnlyDocument(w.ToString());
			ConsistencyChecker.CheckMissingTokens(node, "test.cs", doc);
			ConsistencyChecker.CheckPositionConsistency(node, "test.cs", doc);
		}
Exemplo n.º 15
0
		public XamlAstResolver(ICompilation compilation, XamlFullParseInformation parseInfo)
		{
			if (compilation == null)
				throw new ArgumentNullException("compilation");
			if (parseInfo == null)
				throw new ArgumentNullException("parseInfo");
			this.compilation = compilation;
			this.parseInfo = parseInfo;
			this.textDocument = new ReadOnlyDocument(parseInfo.Text, parseInfo.FileName);
		}
Exemplo n.º 16
0
		/// <summary>
		/// Creates a new DocumentHighlighter instance.
		/// </summary>
		public DocumentHighlighter(ReadOnlyDocument document, IHighlightingDefinition definition)
		{
			if (document == null)
				throw new ArgumentNullException("document");
			if (definition == null)
				throw new ArgumentNullException("definition");
			this.document = document;
			this.definition = definition;
			InvalidateHighlighting();
		}
Exemplo n.º 17
0
		public void ParseAndCheckPositions()
		{
			CSharpParser parser = new CSharpParser();
			foreach (string fileName in fileNames) {
				var currentDocument = new ReadOnlyDocument(File.ReadAllText(fileName));
				SyntaxTree syntaxTree = parser.Parse(currentDocument, fileName);
				if (parser.HasErrors)
					continue;
				ConsistencyChecker.CheckPositionConsistency(syntaxTree, fileName, currentDocument);
				ConsistencyChecker.CheckMissingTokens(syntaxTree, fileName, currentDocument);
			}
		}
Exemplo n.º 18
0
		public static ICodeCompletionBinding PrepareDotCompletion(string expressionToComplete, DebuggerCompletionContext context)
		{
			var lang = SD.LanguageService.GetLanguageByFileName(context.FileName);
			if (lang == null)
				return null;
			string content = GeneratePartialClassContextStub(context);
			const string caretPoint = "$__Caret_Point__$;";
			int caretOffset = content.IndexOf(caretPoint, StringComparison.Ordinal) + expressionToComplete.Length;
			SD.Log.DebugFormatted("context used for dot completion: {0}", content.Replace(caretPoint, "$" + expressionToComplete + "|$"));
			var doc = new ReadOnlyDocument(content.Replace(caretPoint, expressionToComplete));
			return lang.CreateCompletionBinding(context.FileName, doc.GetLocation(caretOffset), doc.CreateSnapshot());
		}
Exemplo n.º 19
0
		public void ParseAndCheckPositions()
		{
			CSharpParser parser = new CSharpParser();
			foreach (string fileName in fileNames) {
				this.currentDocument = new ReadOnlyDocument(File.ReadAllText(fileName));
				CompilationUnit cu = parser.Parse(currentDocument.CreateReader(), fileName);
				if (parser.HasErrors)
					continue;
				this.currentFileName = fileName;
				CheckPositionConsistency(cu);
				CheckMissingTokens(cu);
			}
		}
Exemplo n.º 20
0
		public static void Roundtrip(CSharpParser parser, string fileName, string code)
		{
			// 1. Parse
			CompilationUnit cu = parser.Parse(code, fileName);
			if (parser.HasErrors)
				throw new InvalidOperationException("There were parse errors.");
			
			// 2. Output
			StringWriter w = new StringWriter();
			cu.AcceptVisitor(new CSharpOutputVisitor(w, FormattingOptionsFactory.CreateMono ()));
			string generatedCode = w.ToString().TrimEnd();
			
			// 3. Compare output with original (modulo whitespaces)
			int pos2 = 0;
			for (int pos1 = 0; pos1 < code.Length; pos1++) {
				if (!char.IsWhiteSpace(code[pos1])) {
					while (pos2 < generatedCode.Length && char.IsWhiteSpace(generatedCode[pos2]))
						pos2++;
					if (pos2 >= generatedCode.Length || code[pos1] != generatedCode[pos2]) {
						ReadOnlyDocument doc = new ReadOnlyDocument(code);
						File.WriteAllText(Path.Combine(Program.TempPath, "roundtrip-error.cs"), generatedCode);
						throw new InvalidOperationException("Mismatch at " + doc.GetLocation(pos1) + " of file " + fileName);
					}
					pos2++;
				}
			}
			if (pos2 != generatedCode.Length)
				throw new InvalidOperationException("Mismatch at end of file " + fileName);
			
			// 3b - validate that there are no lone \r
			if (generatedCode.Replace(w.NewLine, "\n").IndexOf('\r') >= 0)
				throw new InvalidOperationException(@"Got lone \r in " + fileName);
			
			// 4. Parse generated output
			CompilationUnit generatedCU;
			try {
				generatedCU = parser.Parse(generatedCode, fileName);
			} catch {
				File.WriteAllText(Path.Combine(Program.TempPath, "roundtrip-error.cs"), generatedCode, Encoding.Unicode);
				throw;
			}
			
			if (parser.HasErrors) {
				File.WriteAllText(Path.Combine(Program.TempPath, "roundtrip-error.cs"), generatedCode);
				throw new InvalidOperationException("There were parse errors in the roundtripped " + fileName);
			}
			
			// 5. Compare AST1 with AST2
			if (!cu.IsMatch(generatedCU))
				throw new InvalidOperationException("AST match failed for " + fileName + ".");
		}
Exemplo n.º 21
0
		public static XamlUnresolvedFile Create(FileName fileName, ITextSource fileContent, AXmlDocument document)
		{
			XamlUnresolvedFile file = new XamlUnresolvedFile(fileName);
			ReadOnlyDocument textDocument = new ReadOnlyDocument(fileContent, fileName);
			
			file.errors.AddRange(document.SyntaxErrors.Select(err => new Error(ErrorType.Error, err.Description, textDocument.GetLocation(err.StartOffset))));
			var visitor = new XamlDocumentVisitor(file, textDocument);
			visitor.VisitDocument(document);
			if (visitor.TypeDefinition != null)
				file.topLevel = new[] { visitor.TypeDefinition };
			else
				file.topLevel = new IUnresolvedTypeDefinition[0];
			
			return file;
		}
Exemplo n.º 22
0
		public void EmptyReadOnlyDocument()
		{
			IDocument document = new ReadOnlyDocument(string.Empty);
			Assert.AreEqual(string.Empty, document.Text);
			Assert.AreEqual(0, document.TextLength);
			Assert.AreEqual(1, document.LineCount);
			Assert.AreEqual(0, document.GetOffset(1, 1));
			Assert.AreEqual(new TextLocation(1, 1), document.GetLocation(0));
			
			Assert.AreEqual(0, document.GetLineByNumber(1).Offset);
			Assert.AreEqual(0, document.GetLineByNumber(1).EndOffset);
			Assert.AreEqual(0, document.GetLineByNumber(1).Length);
			Assert.AreEqual(0, document.GetLineByNumber(1).TotalLength);
			Assert.AreEqual(0, document.GetLineByNumber(1).DelimiterLength);
			Assert.AreEqual(1, document.GetLineByNumber(1).LineNumber);
		}
Exemplo n.º 23
0
        public void EmptyReadOnlyDocument()
        {
            IDocument document = new ReadOnlyDocument(string.Empty);

            Assert.AreEqual(string.Empty, document.Text);
            Assert.AreEqual(0, document.TextLength);
            Assert.AreEqual(1, document.LineCount);
            Assert.AreEqual(0, document.GetOffset(1, 1));
            Assert.AreEqual(new TextLocation(1, 1), document.GetLocation(0));

            Assert.AreEqual(0, document.GetLineByNumber(1).Offset);
            Assert.AreEqual(0, document.GetLineByNumber(1).EndOffset);
            Assert.AreEqual(0, document.GetLineByNumber(1).Length);
            Assert.AreEqual(0, document.GetLineByNumber(1).TotalLength);
            Assert.AreEqual(0, document.GetLineByNumber(1).DelimiterLength);
            Assert.AreEqual(1, document.GetLineByNumber(1).LineNumber);
        }
Exemplo n.º 24
0
		public static void CheckMissingTokens(AstNode node, string currentFileName, IDocument currentDocument = null)
		{
			if (currentDocument == null)
				currentDocument = new ReadOnlyDocument(File.ReadAllText(currentFileName));
			if (IsLeafNode(node)) {
				Assert.IsNull(node.FirstChild, "Token nodes should not have children");
			} else {
				var prevNodeEnd = node.StartLocation;
				var prevNode = node;
				for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
					CheckWhitespace(prevNode, prevNodeEnd, child, child.StartLocation, currentFileName, currentDocument);
					CheckMissingTokens(child, currentFileName, currentDocument);
					prevNode = child;
					prevNodeEnd = child.EndLocation;
				}
				CheckWhitespace(prevNode, prevNodeEnd, node, node.EndLocation, currentFileName, currentDocument);
			}
		}
        public override void FixtureSetUp()
        {
            base.FixtureSetUp();
            SD.Services.AddService(typeof(IWorkbench), MockRepository.GenerateStub<IWorkbench>());

            // Set up SearchOptions required by the BruteForceSearchStrategy.
            SearchOptions.CurrentFindPattern = "foo";
            SearchOptions.MatchCase = false;
            SearchOptions.MatchWholeWord = false;

            // Create the document to be searched.
            var doc = new ReadOnlyDocument("foo");

            var location = MockRepository.GenerateStub<SearchLocation>(SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories, null);

            location.Stub(l => l.GenerateFileList()).Return(new[] { new FileName(@"C:\Temp\test.txt") });

            // Search the document.
            var strategy = SearchStrategyFactory.Create(SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchMode);
            result = SearchManager.FindNext(strategy, location);
        }
Exemplo n.º 26
0
		public static IDocumentIndentEngine CreateEngine(string text, CSharpFormattingOptions formatOptions = null, IEnumerable<string> symbols = null)
		{
			var policy = formatOptions;
			if ( policy == null) {
				policy = FormattingOptionsFactory.CreateMono();
				policy.IndentPreprocessorDirectives = false;
				policy.AlignToFirstMethodCallArgument = policy.AlignToFirstIndexerArgument = true;

			}

			var sb = new StringBuilder();
			int offset = 0;
			for (int i = 0; i < text.Length; i++)
			{
				var ch = text[i];
				if (ch == '$')
				{
					offset = i;
					continue;
				}
				sb.Append(ch);
			}

			var document = new ReadOnlyDocument(sb.ToString());
			var options = new TextEditorOptions();

			var csi = new CSharpIndentEngine(document, options, policy) {
				EnableCustomIndentLevels = true
			};
			if (symbols != null) {
				foreach (var sym in symbols) {
					csi.DefineSymbol(sym);
				}
			}
			var result = new CacheIndentEngine(csi);
			result.Update(offset);
			return result;
		}
Exemplo n.º 27
0
		public static void CheckPositionConsistency (AstNode node, string currentFileName, IDocument currentDocument = null)
		{
			if (currentDocument == null)
				currentDocument = new ReadOnlyDocument(File.ReadAllText(currentFileName));
			string comment = "(" + node.GetType ().Name + " at " + node.StartLocation + " in " + currentFileName + ")";
			var pred = node.StartLocation <= node.EndLocation;
			if (!pred)
				PrintNode (node);
			Assert.IsTrue(pred, "StartLocation must be before EndLocation " + comment);
			var prevNodeEnd = node.StartLocation;
			var prevNode = node;
			for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
				bool assertion = child.StartLocation >= prevNodeEnd;
				if (!assertion) {
					PrintNode (prevNode);
					PrintNode (node);
				}
				Assert.IsTrue(assertion, currentFileName + ": Child " + child.GetType () +" (" + child.StartLocation  + ")" +" must start after previous sibling " + prevNode.GetType () + "(" + prevNode.StartLocation + ")");
				CheckPositionConsistency(child, currentFileName, currentDocument);
				prevNodeEnd = child.EndLocation;
				prevNode = child;
			}
			Assert.IsTrue(prevNodeEnd <= node.EndLocation, "Last child must end before parent node ends " + comment);
		}
		static int GetIndex(string text)
		{
			var editorText = new StringBuilder();
			int trigger = 0, end = 0;
			for (int i = 0; i < text.Length; i++) {
				if (text[i] == '@') {
					trigger = editorText.Length;
					continue;
				}
				if (text[i] == '$') {
					end = editorText.Length;
					continue;
				}
				editorText.Append(text [i]);
			}

			var doc = new ReadOnlyDocument(editorText.ToString ());
			var pctx = new CSharpProjectContent();
			var rctx = new CSharpTypeResolveContext(pctx.CreateCompilation().MainAssembly);
			var ctxProvider = new DefaultCompletionContextProvider(doc, new CSharpUnresolvedFile());
			var engine = new CSharpParameterCompletionEngine(doc, ctxProvider, new ParameterCompletionTests.TestFactory(pctx), pctx, rctx);

			return engine.GetCurrentParameterIndex(trigger, end);
		}
Exemplo n.º 29
0
		public static void ReadAndTest(string filePath, CSharpFormattingOptions policy = null, TextEditorOptions options = null)
		{
			if (File.Exists(filePath))
			{
				filePath = Path.GetFullPath(filePath);
				var code = File.ReadAllText(filePath);
				var document = new ReadOnlyDocument(code);
				if (policy == null) {
					policy = FormattingOptionsFactory.CreateMono();
					policy.AlignToFirstIndexerArgument = policy.AlignToFirstMethodCallArgument = true;
				}
				options = options ?? new TextEditorOptions { IndentBlankLines = false };

				var engine = new CacheIndentEngine(new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true });
				int errors = 0;

				foreach (var ch in code)
				{
					if (options.EolMarker[0] == ch)
					{
						if (!(engine.LineBeganInsideMultiLineComment || engine.LineBeganInsideVerbatimString)) {
							if (engine.CurrentIndent.Length > 0) {
								if (engine.NeedsReindent) {
									errors++;
									Console.WriteLine(string.Format("Indent: {2}, Current indent: {3} in {0}:{1}", filePath, engine.Location.Line, engine.ThisLineIndent.Length, engine.CurrentIndent.Length));
								}
							}
						}
					}

					engine.Push(ch);
				}
				Assert.AreEqual(0, errors);

			}
			else
			{
				Assert.Fail("File " + filePath + " doesn't exist.");
			}
		}
Exemplo n.º 30
0
		static CompletionDataList CreateProvider (string text, CompilationUnit compilationUnit, CSharpCompletionEngine engine, ReadOnlyDocument doc, TextLocation loc)
		{
			var cursorPosition = doc.GetOffset (loc);
			
			var data = engine.GetCompletionData (cursorPosition, true);
			
			return new CompletionDataList () {
				Data = data,
				AutoCompleteEmptyMatch = engine.AutoCompleteEmptyMatch,
				AutoSelect = engine.AutoSelect,
				DefaultCompletionString = engine.DefaultCompletionString
			};
		}
Exemplo n.º 31
0
		Tuple<ReadOnlyDocument, CSharpCompletionEngine> GetContent (string text, CompilationUnit compilationUnit)
		{
			var doc = new ReadOnlyDocument (text);
			IProjectContent pctx = new CSharpProjectContent ();
			pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore });
			var parsedFile = compilationUnit.ToTypeSystem ();
			
			pctx = pctx.UpdateProjectContent (null, parsedFile);
			var cmp = pctx.CreateCompilation ();
			
			var engine = new CSharpCompletionEngine (doc, new TestFactory (), pctx, new CSharpTypeResolveContext (cmp.MainAssembly), compilationUnit, parsedFile);
			engine.EolMarker = Environment.NewLine;
			engine.FormattingPolicy = FormattingOptionsFactory.CreateMono ();
			return Tuple.Create (doc, engine);
		}
Exemplo n.º 32
0
		static CompletionDataList CreateProvider (string text, bool isCtrlSpace)
		{
			string parsedText;
			string editorText;
			int cursorPosition = text.IndexOf ('$');
			int endPos = text.IndexOf ('$', cursorPosition + 1);
			if (endPos == -1) {
				parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			} else {
				parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1);
				editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1);
				cursorPosition = endPos - 1; 
			}
			var doc = new ReadOnlyDocument (editorText);
			
			IProjectContent pctx = new CSharpProjectContent ();
			pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore });
			
			var compilationUnit = new CSharpParser ().Parse (parsedText, "program.cs");
			compilationUnit.Freeze ();
			
			var parsedFile = compilationUnit.ToTypeSystem ();
			pctx = pctx.UpdateProjectContent (null, parsedFile);
			
			var cmp = pctx.CreateCompilation ();
			var loc = doc.GetLocation (cursorPosition);
			
			var rctx = new CSharpTypeResolveContext (cmp.MainAssembly);
			rctx = rctx.WithUsingScope (parsedFile.GetUsingScope (loc).Resolve (cmp));
			

			var curDef = parsedFile.GetInnermostTypeDefinition (loc);
			if (curDef != null) {
				var resolvedDef = curDef.Resolve (rctx).GetDefinition ();
				rctx = rctx.WithCurrentTypeDefinition (resolvedDef);
				var curMember = resolvedDef.Members.FirstOrDefault (m => m.Region.Begin <= loc && loc < m.BodyRegion.End);
				if (curMember != null)
					rctx = rctx.WithCurrentMember (curMember);
			}
			var engine = new CSharpCompletionEngine (doc, new TestFactory (), pctx, rctx, compilationUnit, parsedFile);
				
			engine.EolMarker = Environment.NewLine;
			engine.FormattingPolicy = FormattingOptionsFactory.CreateMono ();
			
			var data = engine.GetCompletionData (cursorPosition, isCtrlSpace);
			
			return new CompletionDataList () {
				Data = data,
				AutoCompleteEmptyMatch = engine.AutoCompleteEmptyMatch,
				AutoSelect = engine.AutoSelect,
				DefaultCompletionString = engine.DefaultCompletionString
			};
		}
Exemplo n.º 33
0
		void AddCommentTags(SyntaxTree cu, IList<TagComment> tagComments, ITextSource fileContent, FileName fileName, ref IDocument document)
		{
			foreach (var comment in cu.Descendants.OfType<Comment>()) {
				if (comment.CommentType == CommentType.InactiveCode)
					continue;
				string match;
				if (comment.Content.ContainsAny(TaskListTokens, 0, out match)) {
					if (document == null)
						document = new ReadOnlyDocument(fileContent, fileName);
					int commentSignLength = comment.CommentType == CommentType.Documentation || comment.CommentType == CommentType.MultiLineDocumentation ? 3 : 2;
					int commentEndSignLength = comment.CommentType == CommentType.MultiLine || comment.CommentType == CommentType.MultiLineDocumentation ? 2 : 0;
					int commentStartOffset = document.GetOffset(comment.StartLocation) + commentSignLength;
					int commentEndOffset = document.GetOffset(comment.EndLocation) - commentEndSignLength;
					int endOffset;
					int searchOffset = 0;
					do {
						int start = commentStartOffset + searchOffset;
						int absoluteOffset = document.IndexOf(match, start, document.TextLength - start, StringComparison.Ordinal);
						var startLocation = document.GetLocation(absoluteOffset);
						endOffset = Math.Min(document.GetLineByNumber(startLocation.Line).EndOffset, commentEndOffset);
						string content = document.GetText(absoluteOffset, endOffset - absoluteOffset);
						if (content.Length < match.Length) {
							// HACK: workaround parser bug with multi-line documentation comments
							break;
						}
						tagComments.Add(new TagComment(content.Substring(0, match.Length), new DomRegion(cu.FileName, startLocation.Line, startLocation.Column), content.Substring(match.Length)));
						searchOffset = endOffset - commentStartOffset;
					} while (comment.Content.ContainsAny(TaskListTokens, searchOffset, out match));
				}
			}
		}
Exemplo n.º 34
0
		public void FindLocalReferences(ParseInformation parseInfo, ITextSource fileContent, IVariable variable, ICompilation compilation, Action<SearchResultMatch> callback, CancellationToken cancellationToken)
		{
			var csParseInfo = parseInfo as CSharpFullParseInformation;
			if (csParseInfo == null)
				throw new ArgumentException("Parse info does not have SyntaxTree");
			
			ReadOnlyDocument document = null;
			IHighlighter highlighter = null;
			
			new FindReferences().FindLocalReferences(
				variable, csParseInfo.UnresolvedFile, csParseInfo.SyntaxTree, compilation,
				delegate (AstNode node, ResolveResult result) {
					if (document == null) {
						document = new ReadOnlyDocument(fileContent, parseInfo.FileName);
						highlighter = SD.EditorControlService.CreateHighlighter(document);
						highlighter.BeginHighlighting();
					}
					var region = new DomRegion(parseInfo.FileName, node.StartLocation, node.EndLocation);
					int offset = document.GetOffset(node.StartLocation);
					int length = document.GetOffset(node.EndLocation) - offset;
					var builder = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter);
					var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null;
					callback(new SearchResultMatch(parseInfo.FileName, node.StartLocation, node.EndLocation, offset, length, builder, defaultTextColor));
				}, cancellationToken);
			
			if (highlighter != null) {
				highlighter.Dispose();
			}
		}