Exemplo n.º 1
0
        public override IScanner GetScanner(IVsTextLines buffer)
        {
            string filePath = FilePathUtilities.GetFilePath(buffer);

            // Return dynamic scanner based on file extension
            return(NShaderScannerFactory.GetShaderScanner(filePath));
        }
        public void VsTextViewCreated(IVsTextView textViewAdapter)
        {
            IVsTextLines textlines;

            textViewAdapter.GetBuffer(out textlines);
            if (textlines != null)
            {
                Guid langId;
                textlines.GetLanguageServiceID(out langId);
                if (langId == GuidStrings.guidLanguageService)          // is our language service active ?
                {
                    string fileName = FilePathUtilities.GetFilePath(textlines);
                    if (!IsOurFile(fileName))       // is this a file node from Vulcan ?
                    {
                        Guid guidVulcanLanguageService = GuidStrings.guidVulcanLanguageService;
                        textlines.SetLanguageServiceID(guidVulcanLanguageService);
                        return;
                    }
                    //
                    // Only capturing keystroke for OUR languageService... ???
                    //
                    IWpfTextView textView = AdaptersFactory.GetWpfTextView(textViewAdapter);
                    Debug.Assert(textView != null);
                    CommandFilter     filter = new CommandFilter(textView, CompletionBroker, NavigatorService.GetTextStructureNavigator(textView.TextBuffer), SignatureHelpBroker, aggregator);
                    IOleCommandTarget next;
                    textViewAdapter.AddCommandFilter(filter, out next);
                    filter.Next = next;
                }
            }
        }
Exemplo n.º 3
0
        private DocumentId GetDocumentId(IVsTextLines buffer)
        {
            var filePath  = FilePathUtilities.GetFilePath(buffer) + ".cs";
            var documents = _workspace
                            .CurrentSolution
                            .GetDocumentIdsWithFilePath(filePath);

            return(documents.SingleOrDefault());
        }
Exemplo n.º 4
0
        internal void SetBufferCoordinator(IVsTextBufferCoordinator coordinator)
        {
            _bufferCoordinator = coordinator;

            var primaryBuffer = GetPrimaryTextLines();

            _primaryFileIndex = Location.GetFileIndex(FilePathUtilities.GetFilePath(primaryBuffer));
            _primaryToSecondaryFilesMap[_primaryFileIndex] = FileIndex;
        }
Exemplo n.º 5
0
        public override Source CreateSource(IVsTextLines buffer)
        {
            var filePath = FilePathUtilities.GetFilePath(buffer) + ".cs";
            ImmutableArray <DocumentId> documents = _workspace.CurrentSolution.GetDocumentIdsWithFilePath(filePath);

            var documentId = documents[0];

            return(new ExcessSource(this, buffer, new Colorizer(this, buffer, GetScanner(buffer)), _workspace, documentId));
        }
Exemplo n.º 6
0
        public override IScanner GetScanner(IVsTextLines buffer)
        {
            // Since Visual Studio handles language service associations by file
            // extension, CMakeLanguageService must handle all *.txt files in order to
            // handle CMakeLists.txt.  Detect if the buffer represents an ordinary text
            // files.  If so, disable syntax highlighting.  This is a kludge, but it's
            // the best that can be done here.
            string path = FilePathUtilities.GetFilePath(buffer);

            return(new CMakeScanner(!CMakeSource.IsCMakeFile(path)));
        }
Exemplo n.º 7
0
        public override Source CreateSource(IVsTextLines buffer)
        {
            var filePath = FilePathUtilities.GetFilePath(buffer);

            if (GlobalServices.GetProjectManagerForFile(filePath) == null)
            {
                return(base.CreateSource(buffer));
            }
            return(new BooSource(this, filePath, buffer, GetColorizer(buffer))
            {
                LastParseTime = 0
            });
        }
Exemplo n.º 8
0
        public void VsTextViewCreated(IVsTextView textViewAdapter)
        {
            IVsTextLines textlines;

            textViewAdapter.GetBuffer(out textlines);
            if (textlines != null)
            {
                Guid langId;
                textlines.GetLanguageServiceID(out langId);
                IWpfTextView textView = AdaptersFactory.GetWpfTextView(textViewAdapter);
                Debug.Assert(textView != null);
                // Note that this may get called after the classifier has been instantiated

                if (langId == GuidStrings.guidLanguageService)          // is our language service active ?
                {
                    string fileName = FilePathUtilities.GetFilePath(textlines);
                    if (!IsOurSourceFile(fileName))       // is this a file node from Vulcan ?
                    {
                        // we always register the file in the classifier.
                        if (textView.TextBuffer.Properties.ContainsProperty(typeof(XSharpModel.XFile)))
                        {
                            textView.TextBuffer.Properties.RemoveProperty(typeof(XSharpModel.XFile));
                        }

                        Guid guidVulcanLanguageService = GuidStrings.guidVulcanLanguageService;
                        textlines.SetLanguageServiceID(guidVulcanLanguageService);
                        return;
                    }
                    //
                    // Only capturing keystroke for OUR languageService... ???
                    //

                    // Get XFile and assign it to the textbuffer
                    if (!textView.TextBuffer.Properties.ContainsProperty(typeof(XSharpModel.XFile)))
                    {
                        var file = XSharpModel.XSolution.FindFile(fileName);
                        if (file != null)
                        {
                            textView.TextBuffer.Properties.AddProperty(typeof(XSharpModel.XFile), file);
                            file.Interactive = true;
                        }
                    }
                    CommandFilter     filter = new CommandFilter(textView, CompletionBroker, NavigatorService.GetTextStructureNavigator(textView.TextBuffer), SignatureHelpBroker, aggregator, this);
                    IOleCommandTarget next;
                    textViewAdapter.AddCommandFilter(filter, out next);
                    filter.Next = next;
                }
            }
        }
Exemplo n.º 9
0
 public static AsmHighlighterScanner GetScanner(IVsTextLines buffer)
 {
     return(GetScanner(FilePathUtilities.GetFilePath(buffer)));
 }
Exemplo n.º 10
0
 public static NShaderScanner GetShaderScanner(IVsTextLines buffer)
 {
     return(GetShaderScanner(FilePathUtilities.GetFilePath(buffer)));
 }
Exemplo n.º 11
0
        public static List <EditSpan> ReformatCode(IVsTextLines pBuffer, TextSpan span, int tabSize)
        {
            string filePath = FilePathUtilities.GetFilePath(pBuffer);

            // Return dynamic scanner based on file extension
            List <EditSpan> changeList = new List <EditSpan>();

            string codeToFormat;

            int endOfFirstLineIndex;

            // Get 1st line and parse custom define
            pBuffer.GetLengthOfLine(0, out endOfFirstLineIndex);
            pBuffer.GetLineText(0, 0, 0, endOfFirstLineIndex, out codeToFormat);

            Dictionary <string, string> defines = ParseDefineLine(codeToFormat);

            AsmHighlighterScanner scanner = AsmHighlighterScannerFactory.GetScanner(filePath);
            Scanner lexer = scanner.Lexer;


            // Iterate on each line of the selection to format
            for (int line = span.iStartLine; line <= span.iEndLine; line++)
            {
                int lineLength;
                pBuffer.GetLengthOfLine(line, out lineLength);
                pBuffer.GetLineText(line, 0, line, lineLength, out codeToFormat);

                string codeToAssemble = ConvertToFasm(lexer, codeToFormat, defines);

                lexer.SetSource(codeToFormat, 0);
                int state = 0;
                int start, end;

                bool instructionFound = false, commentFound = false;
                int  commentStart         = 0;
                AsmHighlighterToken token = (AsmHighlighterToken)lexer.GetNext(ref state, out start, out end);
                while (token != AsmHighlighterToken.EOF)
                {
                    switch (token)
                    {
                    case AsmHighlighterToken.INSTRUCTION:
                    case AsmHighlighterToken.FPUPROCESSOR:
                    case AsmHighlighterToken.SIMDPROCESSOR:
                        instructionFound = true;
                        break;

                    case AsmHighlighterToken.COMMENT_LINE:
                        if (!commentFound)
                        {
                            commentFound = true;
                            commentStart = start;
                        }
                        break;
                    }

                    if (instructionFound && commentFound)
                    {
                        byte[] buffer = null;

                        try
                        {
                            buffer = ManagedFasm.Assemble(codeToAssemble);
                        }
                        catch (Exception ex)
                        {
                            // Unable to parse instruction... skip
                        }
                        if (buffer != null)
                        {
                        }

                        TextSpan editTextSpan = new TextSpan();
                        editTextSpan.iStartLine  = line;
                        editTextSpan.iEndLine    = line;
                        editTextSpan.iStartIndex = commentStart;
                        editTextSpan.iEndIndex   = commentStart + 1;
                        if ((codeToFormat.Length - commentStart) > 2 && codeToFormat.Substring(commentStart, 2) == ";#")
                        {
                            editTextSpan.iEndIndex = editTextSpan.iEndIndex + 2;
                        }

                        string text = ";#" + ((buffer == null) ? "?" : string.Format("{0:X}", buffer.Length));
                        changeList.Add(new EditSpan(editTextSpan, text));
                        break;
                    }
                    token = (AsmHighlighterToken)lexer.GetNext(ref state, out start, out end);
                }
            }

            return(changeList);
        }
Exemplo n.º 12
0
        public static List <EditSpan> ReformatCode(IVsTextLines pBuffer, TextSpan span, int tabSize)
        {
            string filePath = FilePathUtilities.GetFilePath(pBuffer);

            // Return dynamic scanner based on file extension

            List <EditSpan> changeList = new List <EditSpan>();
            int             nbLines;

            pBuffer.GetLineCount(out nbLines);
            string codeToFormat;

            int lastLine;
            int lastLineIndex;

            pBuffer.GetLastLineIndex(out lastLine, out lastLineIndex);
            pBuffer.GetLineText(0, 0, lastLine, lastLineIndex, out codeToFormat);

            NShaderScanner shaderScanner = NShaderScannerFactory.GetShaderScanner(filePath);
            Scanner        lexer         = shaderScanner.Lexer;

            lexer.SetSource(codeToFormat, 0);

            int spanStart;
            int spanEnd;

            pBuffer.GetPositionOfLineIndex(span.iStartLine, span.iStartIndex, out spanStart);
            pBuffer.GetPositionOfLineIndex(span.iEndLine, span.iEndIndex, out spanEnd);

            int         state = 0;
            int         start, end;
            ShaderToken token = (ShaderToken)lexer.GetNext(ref state, out start, out end);

            List <int> brackets   = new List <int>();
            List <int> delimiters = new List <int>();
            // !EOL and !EOF
            int level            = 0;
            int startCopy        = 0;
            int levelParenthesis = 0;

            while (token != ShaderToken.EOF)
            {
                switch (token)
                {
                case ShaderToken.LEFT_PARENTHESIS:
                    levelParenthesis++;
                    break;

                case ShaderToken.RIGHT_PARENTHESIS:
                    levelParenthesis--;
                    if (levelParenthesis < 0)
                    {
                        levelParenthesis = 0;
                    }
                    break;

                case ShaderToken.LEFT_BRACKET:
                    level++;
                    if (codeToFormat[start] == '{' && start >= spanStart && end <= spanEnd)
                    {
                        Match match = matchBraceStart.Match(codeToFormat, start);


                        StringBuilder codeFormatted = new StringBuilder();
                        codeFormatted.Append("{\r\n");
                        int levelToIndentNext = level;
                        if (match.Groups.Count == 2)
                        {
                            string matchStr = match.Groups[1].Value;
                            levelToIndentNext--;
                        }
                        for (int i = 0; i < levelToIndentNext; i++)
                        {
                            for (int j = 0; j < tabSize; j++)
                            {
                                codeFormatted.Append(' ');
                            }
                        }
                        if (match.Groups.Count == 2)
                        {
                            codeFormatted.Append("}\r\n");
                        }

                        TextSpan editTextSpan = new TextSpan();

                        pBuffer.GetLineIndexOfPosition(start,
                                                       out editTextSpan.iStartLine,
                                                       out editTextSpan.iStartIndex);
                        pBuffer.GetLineIndexOfPosition(startCopy + match.Index + match.Length,
                                                       out editTextSpan.iEndLine,
                                                       out editTextSpan.iEndIndex);

                        changeList.Add(new EditSpan(editTextSpan, codeFormatted.ToString()));
                    }
                    break;

                case ShaderToken.RIGHT_BRACKET:
                    level--;
                    if (level < 0)
                    {
                        level = 0;
                    }
                    brackets.Add(start);
                    break;

                case ShaderToken.DELIMITER:
                    if (codeToFormat[start] == ';' && start >= spanStart && end <= spanEnd && levelParenthesis == 0)
                    {
                        Match match = matchEndOfStatement.Match(codeToFormat, start);

                        StringBuilder codeFormatted = new StringBuilder();
                        codeFormatted.Append(";\r\n");
                        int  levelToIndentNext = level;
                        bool isBracketFound    = (match.Groups.Count == 2 && match.Groups[1].Value == "}");
                        if (isBracketFound)
                        {
                            string matchStr = match.Groups[1].Value;
                            levelToIndentNext--;
                        }
                        for (int i = 0; i < levelToIndentNext; i++)
                        {
                            for (int j = 0; j < tabSize; j++)
                            {
                                codeFormatted.Append(' ');
                            }
                        }
                        if (isBracketFound)
                        {
                            codeFormatted.Append("}\r\n");
                        }

                        TextSpan editTextSpan = new TextSpan();

                        pBuffer.GetLineIndexOfPosition(start,
                                                       out editTextSpan.iStartLine,
                                                       out editTextSpan.iStartIndex);
                        pBuffer.GetLineIndexOfPosition(startCopy + match.Index + match.Length,
                                                       out editTextSpan.iEndLine,
                                                       out editTextSpan.iEndIndex);

                        changeList.Add(new EditSpan(editTextSpan, codeFormatted.ToString()));
                    }
                    break;
                }
                token = (ShaderToken)lexer.GetNext(ref state, out start, out end);
            }
            return(changeList);
        }
        public void VsTextViewCreated(IVsTextView textViewAdapter)
        {
            IWpfTextView  textView   = EditorAdaptersFactoryService.GetWpfTextView(textViewAdapter);
            IVsTextBuffer textBuffer = EditorAdaptersFactoryService.GetBufferAdapter(textView.TextBuffer);

            textView.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument document);
            textViewAdapter.GetBuffer(out var textlines);
            if (textlines != null)
            {
                XFile  file     = null;
                string fileName = FilePathUtilities.GetFilePath(textlines);
                textlines.GetLanguageServiceID(out var langId);
                // Note that this may get called after the classifier has been instantiated

                if (langId == GuidStrings.guidLanguageService)          // is our language service active ?
                {
                    // Get XFile and assign it to the TextBuffer
                    if (!textView.TextBuffer.Properties.TryGetProperty(typeof(XFile), out file))
                    {
                        file = XSolution.FindFile(fileName);
                        if (file == null)
                        {
                            XSolution.OrphanedFilesProject.AddFile(fileName);
                            file = XSolution.FindFile(fileName);
                        }
                        if (file != null)
                        {
                            textView.TextBuffer.Properties.AddProperty(typeof(XFile), file);
                        }
                    }

                    if (file != null)
                    {
                        file.Interactive = true;
                        textView.Properties.AddProperty(typeof(XFile), file);
                    }
                    var filter = new XSharpEditorCommandHandler(textView);
                    IOleCommandTarget next;
                    textViewAdapter.AddCommandFilter(filter, out next);

                    filter.Next = next;
                }
                // For VS 2017 we look for Microsoft.VisualStudio.Editor.Implementation.VsCodeWindowAdapter
                // For VS 2019 we look for Microsoft.VisualStudio.TextManager.Interop.IVsCodeWindow
                // Both implement IVsDropdownbarManager
                IVsDropdownBarManager dropDownBarManager = null;
                if (dropDownBarKey != null && textView.Properties.ContainsProperty(dropDownBarKey))
                {
                    object window = textView.Properties.GetProperty(dropDownBarKey);
                    dropDownBarManager = window as IVsDropdownBarManager;
                }
                if (dropDownBarManager == null)
                {
                    // look at all the properties to find the one that implements IVsDropdownBarManager
                    foreach (var property in textView.Properties.PropertyList)
                    {
                        if (property.Value is IVsDropdownBarManager manager)
                        {
                            dropDownBarKey     = property.Key; // remember key for next iteration
                            dropDownBarManager = manager;
                            break;
                        }
                    }
                }
                // The same file may be open in multiple textViews
                // these will share the same dropdown
                if (_dropDowns.ContainsKey(fileName))
                {
                    dropdown = _dropDowns[fileName];
                    dropdown.addTextView(textView);
                }
                else if (dropDownBarManager != null)
                {
                    dropdown = new XSharpDropDownClient(dropDownBarManager, file);
                    dropDownBarManager.RemoveDropdownBar();
                    dropDownBarManager.AddDropdownBar(2, dropdown);
                    _dropDowns.Add(fileName, dropdown);
                    dropdown.addTextView(textView);
                }
                if (!_textViews.ContainsKey(fileName))
                {
                    _textViews.Add(fileName, new List <IWpfTextView>());
                }
                _textViews[fileName].Add(textView);
                textView.Closed += TextView_Closed;
            }
        }
Exemplo n.º 14
0
        public int GetTextViewFilter(IVsIntellisenseHost pISenseHost, IOleCommandTarget pNextCmdTarget, out IVsTextViewFilter pTextViewFilter)
        {
            pTextViewFilter = null;

            IVsTextLines buffer;

            ErrorHandler.ThrowOnFailure(bufferCoordinator.GetSecondaryBuffer(out buffer));

            var secondaryFilePath = FilePathUtilities.GetFilePath(buffer);

            if (secondaryFilePath == null)
            {
                secondaryFilePath = NemerleSource.GetStubFileForSecondaryBuffer(buffer);
            }

            var secondaryFileIndex = Location.GetFileIndex(secondaryFilePath);
            var primaryFileindex   = Location.GetFileIndex(_filePath);

            bool doOutlining = LanguageService.Preferences.AutoOutlining;

            LanguageService.Preferences.AutoOutlining = false;

            if (_projectInfo != null && LanguageService.GetSource(buffer) == null)
            {
                // создаем и регистрируем в проекте временный source, чтобы не сломалась логика
                // конструктора NemerleSource (см вызов LanguageService.AddEditableSource)
                //_projectInfo.ReplaseOrAddSource(new FileNemerleSource(secondaryFileIndex));
                _projectInfo.AddEditableSource((NemerleSource)LanguageService.CreateSource(buffer));
            }

            NemerleSource source = LanguageService.GetOrCreateSource(buffer) as NemerleSource;

            source.SetBufferCoordinator(bufferCoordinator);

            if (_projectInfo != null)
            {
                _projectInfo.Engine.RequestOnBuildTypesTree();
            }

            LanguageService.Preferences.AutoOutlining = doOutlining;

            _windowManager = LanguageService.CreateCodeWindowManager(null, source);

            language.AddCodeWindowManager(_windowManager);

            // увеличиваем внутренний счетчик openCount, для того чтобы впоследствии корректно отработала логика закрытия соурса
            source.Open();

            TextViewWrapper view = new TextViewWrapper(languageHost, pISenseHost, bufferCoordinator, pNextCmdTarget, source);

            _windowManager.OnNewView(view);

            pTextViewFilter = view.InstalledFilter;
            NemerleViewFilter nemerleFilter = pTextViewFilter as NemerleViewFilter;

            if (null != nemerleFilter)
            {
                nemerleFilter.BufferCoordinator = this.bufferCoordinator;
            }

            // сохраним значение DocumentEvents в переменной класса, чтобы исключить преждевременное уничтожение
            // объекта и автоматическоей отписывание от событий.
            //
            // Источник решения:
            // PRB: Visual Studio .NET events being disconnected from add-in (http://www.mztools.com/articles/2005/mz2005012.aspx)
            _documentEvents = _projectItem.DTE.Events.get_DocumentEvents(_projectItem.Document);

            _documentEvents.DocumentClosing += _documentClosingEventHandler;

            return(VSConstants.S_OK);
        }