Skip to content

Code Check #10

@SAgiKPJH

Description

@SAgiKPJH

Hmm

  • Code를 저장하는데, 검증을 해야합니다.
  • built, Execute할 땐 허용되는 Keyword가 있는 반면
  • Save할 때 불가능한 키워드가 있습니다.
  • 두 경우 모두 안되는 키워드가 있습니다.

Usage

private void _saveCommand(IDialogView view)
{
    // ...

    if (!_checkRoslynPad() || _checkErrorCode(showMessageBox : true, checkWarning : false, warningToBeError : true))
        return;
        
    // ...
}
private void _buildCommand(IDialogView view)
{
    // ...

    if (!_checkRoslynPad() || _checkErrorCode())
        return;

    // ...
}
private void _executeCommand(IDialogView view)
{
    // ...

    if (!_checkRoslynPad() || _checkErrorCode())
        return;

    // ...
}
private bool _checkErrorCode(bool showMessageBox = true, bool checkWarning = true, bool warningToBeError = false)
{
    var lines = Documents[0].RoslynCodeEditor.Text.Split('\r');

    var checkErrorCodeHelper = new CheckCodeHelper(_logger, _messageBoxService);
    if(checkWarning)
        checkErrorCodeHelper.CheckWarning(lines);

    return checkErrorCodeHelper.IsError(lines, showMessageBox, warningToBeError);
}

Method 1

using Akka.Event;
using System.Collections.Generic;
using IMessageBoxService = Mirero.MLS.Client.Common.Inferface.IMessageBoxService;

namespace Mirero.MLS.Client.View.CreateModel._helper
{
    public class CheckCodeHelper
    {
        private bool _isErrorOrWarning = false;

        private readonly List<string> _warningKeywords = new List<string>()
        {
              "Cv2.WaitKey"
            , "WaitKey"
            , "Cv2.ImShow"
            , "ImShow"
        };

        private readonly List<string> _impossibleKeywords = new List<string>()
        {
              "unsafe"
            , "fixed"
            , "parallel"
            , "AsParallel"
            , "Thread"
            , "Task"
        };

        #region Constructor
        private ILoggingAdapter _logger;
        private IMessageBoxService _messageBoxService;

        public CheckCodeHelper(ILoggingAdapter logger, IMessageBoxService messageBoxService)
        {
            _logger = logger;
            _messageBoxService = messageBoxService;
        }
        #endregion

        public bool IsError(string[] codeLines, bool showMessageBox = true, bool combineWarning = false) => CheckError(codeLines, showMessageBox, combineWarning);

        public bool CheckError(string[] codeLines, bool showMessageBox = true, bool combineWarning = false)
        {
            _isErrorOrWarning = true;

            var errorKeywords = _impossibleKeywords;

            if (combineWarning)
                errorKeywords.AddRange(_warningKeywords);

            return _checkKeyword(codeLines, errorKeywords, showMessageBox);
        }

        public bool CheckWarning(string[] codeLines)
        {
            _isErrorOrWarning = false;

            return _checkKeyword(codeLines, _warningKeywords, showMessageBox: false);
        }

        #region private Method
        private bool _checkKeyword(string[] codeLines, List<string> keywords, bool showMessageBox = true)
        {
            string checkMessage = _getCheckMessage(codeLines, keywords);

            if (checkMessage == "Not Founds")
                return false;

            if (showMessageBox)
                _messageBoxService.Show(checkMessage, "Code " + (_isErrorOrWarning ? "Error" : "Warning"));

            _logging(checkMessage);
            return true;
        }

        private string _getCheckMessage(string[] codeLines, List<string> keywords)
        {
            var findInfo = _findKeyword(codeLines, keywords);

            if (findInfo.LineNum > 0)
                return $"Code {(_isErrorOrWarning ? "Error" : "Warning")}! (Line: {findInfo.LineNum}, Position: {findInfo.FindPos}) Impossible Keyword: {findInfo.KeyWord}";

            return "Not Founds";
        }

        private (int LineNum, int FindPos, string KeyWord) _findKeyword(string[] lines, List<string> keywords)
        {
            for (int lineNum = 0; lineNum < lines.Length; lineNum++)
            {
                var line = lines[lineNum];

                foreach (var key in keywords)
                {
                    int pos = line.IndexOf(key);
                    if (pos > -1 && ((line.IndexOf("//") == -1) || (line.IndexOf("//") > pos)))
                        return (lineNum + 1, pos, key);
                }
            }
            return (0, -1, "Not Founds");
        }

        private void _logging(string message)
        {
            if (_isErrorOrWarning)
                _logger.Error(message);
            else
                _logger.Warning(message + "\nPlease annotate when saving.");
        }
        #endregion
    }
}

Method 2

using Akka.Event;
using System.Collections.Generic;
using IMessageBoxService = Mirero.MLS.Client.Common.Inferface.IMessageBoxService;

namespace Mirero.MLS.Client.View.CreateModel._helper
{
    public class CheckCodeHelper
    {
        private bool _isErrorOrWarning;
        private bool _showMessageBox = false;
        private string[] _codeLines;
        private List<string> _keywords = new List<string>();

        private readonly List<string> _warningKeywords = new List<string>()
        {
              "Cv2.WaitKey"
            , "WaitKey"
            , "Cv2.Imshow"
            , "Imshow"
        };

        private readonly List<string> _impossibleKeywords = new List<string>()
        {
              "unsafe"
            , "fixed"
            , "parallel"
            , "AsParallel"
            , "Thread"
            , "Task"
        };

        #region Constructor
        private ILoggingAdapter _logger;
        private IMessageBoxService _messageBoxService;

        public CheckCodeHelper(ILoggingAdapter logger, IMessageBoxService messageBoxService)
        {
            _logger = logger;
            _messageBoxService = messageBoxService;
        }
        #endregion

        public bool IsError(string[] codeLines, bool showMessageBox = true, bool combineWarning = false) => CheckError(codeLines, showMessageBox, combineWarning);

        public bool CheckError(string[] codeLines, bool showMessageBox = true, bool combineWarning = false)
        {
            var keywords = _impossibleKeywords;
            if (combineWarning)
                keywords.AddRange(_warningKeywords);

            initailizeValue(codeLines, keywords, isErrorOrWarning: true, showMessageBox);

            return _checkKeyword();
        }

        public bool CheckWarning(string[] codeLines)
        {
            initailizeValue(codeLines, _warningKeywords, isErrorOrWarning: false, showMessageBox: false);

            return _checkKeyword();
        }

        #region private Method
        private void initailizeValue(string[] codeLines, List<string> keywords, bool isErrorOrWarning, bool showMessageBox)
        {
            _isErrorOrWarning = isErrorOrWarning;
            _showMessageBox = showMessageBox;
            _codeLines = codeLines;
            _keywords = keywords;
        }

        private bool _checkKeyword()
        {
            string checkMessage = _getCheckMessage();

            if (checkMessage == "Not Founds")
                return false;

            if (_showMessageBox)
                _messageBoxService.Show(checkMessage, "Code " + (_isErrorOrWarning ? "Error" : "Warning"));

            _logging(checkMessage);
            return true;
        }

        private string _getCheckMessage()
        {
            var findInfo = _findKeyword(_codeLines, _warningKeywords);

            if (findInfo.LineNum > 0)
                return $"Code {(_isErrorOrWarning ? "Error" : "Warning")}! (Line: {findInfo.LineNum}, Position: {findInfo.FindPos}) Impossible Keyword: {findInfo.KeyWord}";

            return "Not Founds";
        }

        private (int LineNum, int FindPos, string KeyWord) _findKeyword(string[] lines, List<string> keywords)
        {
            for (int lineNum = 0; lineNum < lines.Length; lineNum++)
            {
                var line = lines[lineNum];

                foreach (var key in keywords)
                {
                    int pos = line.IndexOf(key);
                    if (pos > -1 && ((line.IndexOf("//") == -1) || (line.IndexOf("//") > pos)))
                        return (lineNum + 1, pos, key);
                }
            }
            return (0, -1, "Not Founds");
        }

        private void _logging(string message)
        {
            if (_isErrorOrWarning)
                _logger.Error(message);

            _logger.Warning(message);
        }
        #endregion
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions