代碼評審是軟件質量保證一種活動,由一個或者多個人對一個程序的部分或者全部源代碼進閱讀理解。一般來說分為作者和評審者兩種角色,作者方提供代碼邏輯的介紹和代碼,評審者則對提供的代碼基于設計,功能性和非功能性等方面認知進行閱讀并提出問題。常見的評審組織形式是有同行評審(Peer Review)和小組檢查 (Team Inspection)兩種方式。
在代碼評審中,評審的目的在通過代碼的評審發現潛在的問題,同時分享和表達是代碼評審的重要收獲,我們知道人相同在不同的文化下生產力是不同的,代碼評審是一個工具,工具受文化的影響的同時也影響著文化,最終朝著我們希望的責任共擔、持續改進的方向發展。
4)插件運行調試打包安裝
Gradle構建方式進行調試打包安裝
運行/調試:runIde 可以選擇Debug模式或者是Run模式
打包
安裝:可以將打的包發布市場(本地idea配置插件倉庫),從Marketplace搜索插件或者是直接從Settings->plugins->Install->Install Plugin from Disk安裝
步驟2:研究Gerrit插件源碼,搞清楚整理開發流程和模塊
步驟3:基于Gerrit插件規劃VCR插件模塊,增加clone、branch、mergeRequest、VCR模塊,并對各組件增強
步驟4:定制原有流程模塊push,自動化關聯工作項
在使用Git依賴插件之前,先了解一下插件的擴展以及擴展點(Extensions、Extension Points)。
Intellij 平臺提供了允許一個插件與其他插件或者 IDE 交互的 extensions 以及 extension points 的概念。
可以在 plugin.xml 中的和塊中定義 extensions 以及 extension points。
plugin.xml
<!--依賴插件包--!><depends>Git4Idea</depends><!—idea第一次打開, 實際上就是訂閱了應用程序打開的事件--><application-components><component><implementation-class>com.demo.intellij.plugin.vcr.push.VcrPushExtension$Proxy</implementation-class></component></application-components>
上述我們看到依賴的Git4Idea 包,如果我們想修改原生的的Git,先看下push依賴包中如何實現的。
Git4Idea(plugin.xml)
<extensions defaultExtensionNs="com.intellij"><pushSupport implementation="git4idea.push.GitPushSupport"/>...</extensions>
intellij-dvcs.jar(plugin.xml)
<extensionPoints> <extensionPoint name="pushSupport" interface="com.intellij.dvcs.push.PushSupport" area="IDEA_PROJECT" dynamic="true"/>....</extensionPoints>
從上述可看到,Git4Idea 的GitPushSupport擴展實現push的功能點,接下來我們主要對GitPushSupport進行javassist字節碼修改以達到擴展git push組件能力。
擴展使用GitPushSupport之前,需要將需要的類進行裝載至GitPlugin中,然后再對GitPushSupport進行字節碼改造,至此對git Push原生插件頁進行改造。
步驟5:使用樹狀列表模式,展示一次push請求VCR提交內容及多個CR情況
主要是實現JTreeTable,對VCR與CR進行管理。
一次評審請求VCR包含所有CR的提交變更記錄,可針對該變更記錄進行代碼評審,單個CR也可以進行評審。
步驟6:展示變更文件視圖及定制評論展示模塊,精準定位代碼
代碼評審主要根據編輯器獲取代碼行及位置,評論可精準定位到代碼行。
1)changeBrowser變更視圖展示VCR變更文件信息
2)雙擊文件,diff視圖展示inline和side-by-side兩種代碼差異
聲明擴展,針對擴展類進行定制化改造。
plugin.xml
<diff.DiffTool implementatinotallow="com.demo.intellij.plugin.vcr.ui.diff.VcrCommentsDiffTool$Proxy"/>
3)添加代碼塊評論,定位代碼塊
AddCommentAction.java
public class AddCommentAction extends AnAction implements DumbAware {public AddCommentAction(String label, Icon icon, CommentsDiffTool commentsDiffTool, Editor editor, List<CommentInfo> fileComments .... ) { super(label, null, icon);}private CommentInput createComment() {//獲取用戶選擇代碼位置位置//行的情況下,默認是開頭和行結束 得到光標的位置caretModel.getOffset();/*取到插字光標模式對象 CaretModel caretModel = editor.getCaretModel();得到光標的位置int caretOffset = caretModel.getOffset();//得到一行開始和結束的地方int lineNum = document.getLineNumber(caretOffset);int lineStartOffset = document.getLineStartOffset(lineNum);int lineEndOffset = document.getLineEndOffset(lineNum);獲取一行內容String lineContent = document.getText(new TextRange(lineStartOffset, lineEndOffset));*/Document document = editor.getDocument();int lineNum = document.getLineNumber(editor.getCaretModel().getOffset()) ;int lineStartOffset = document.getLineStartOffset(lineNum);int lineEndOffset = document.getLineEndOffset(lineNum);String lineContent = document.getText(new TextRange(lineStartOffset, lineEndOffset));.....}}
所有評論展示列表如何精準定位代碼
SafeHtmlHistoryComments.java
public class SafeHtmlHistoryComments extends JPanel { private Iterable<CommentInfo> fileComments; private List<CommentInfo> commentInfos = new ArrayList<>(); private CommentInfo currentCommentInfo; private SelectedComment selectedComment; private SelectedComment operatorSelectedComment; private Editor editor; public SafeHtmlHistoryComments(Editor editor,Iterable<CommentInfo> fileComments, Comment selectedComment) { super(new BorderLayout()); .... HistoryCommentListPanel historyCommentListPanel = new HistoryCommentListPanel(fileComments); //雙擊table某行觸發代碼定位 historyCommentListPanel.addTableMouseDoubleHit(new Consumer<CommentInfo>() { @Override public void consume(CommentInfo commentInfo) { codeTextHit(editor,commentInfo); } }); } /** * 定位代碼 * @param editor * @param commentInfo */ private static void codeTextHit(Editor editor, CommentInfo commentInfo) { SelectionModel selectionModel = editor.getSelectionModel(); // 優化:如果文件修改過了,則不進行選中操作,換為提示 if (null != commentInfo.startIndex && null != commentInfo.endIndex && commentInfo.startIndex != 0 && commentInfo.endIndex != 0) { editor.getCaretModel().moveToOffset(commentInfo.endIndex); selectionModel.setSelection(commentInfo.startIndex, commentInfo.endIndex); } else if (null != commentInfo.line && commentInfo.line != 0) { int lineNum = commentInfo.line - 1; editor.getCaretModel().moveToOffset(lineNum); CharSequence charsSequence = editor.getMarkupModel().getDocument().getCharsSequence(); if(null!=commentInfo.range) { RangeUtils.Offset offset = RangeUtils.rangeToTextOffset(charsSequence, commentInfo.range); selectionModel.setSelection(offset.start, offset.end); }else{ Document document = editor.getDocument(); int lineStartOffset = document.getLineStartOffset(lineNum); int lineEndOffset = document.getLineEndOffset(lineNum); selectionModel.setSelection(lineStartOffset, lineEndOffset); } } editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); }....}
本文鏈接:http://www.tebozhan.com/showinfo-26-96989-0.htmlvivo 互聯網自研代碼評審 VCR 落地實踐
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com