diff --git a/README.md b/README.md index 9a77010..bff5553 100644 --- a/README.md +++ b/README.md @@ -4,23 +4,25 @@ Markdown table features. ## 1. Features -| Title | Command | Default Keybinding | In the Editor Right Click Menu | -| :------------------------- | :------------------------ | :----------------- | :---------------------------------- | -| Format all tables. | markdowntable.format | Shift + Alt + F | Yes | -| Convert TSV to table. | markdowntable.tsvToTable | Shift + Alt + T | Yes (only when selecting range) | -| Navigate to next cell. | markdowntable.nextCell | Tab | | -| Navigate to previous cell. | markdowntable.prevCell | Shift + Tab | | -| Insert column to the right.| markdowntable.insertRight | | Yes (only when not selecting range) | -| Insert column to the left. | markdowntable.insertLeft | | Yes (only when not selecting range) | -| Align to Left. | markdowntable.alignLeft | | Yes | -| Align to Center. | markdowntable.alignCenter | | Yes | -| Align to Right. | markdowntable.alignRight | | Yes | +| Title | Command | Default Keybinding | In the Editor Right Click Menu | +| :-------------------------------------- | :---------------------------------- | :----------------- | :---------------------------------- | +| Format all tables. | markdowntable.format | Shift + Alt + F | Yes | +| Convert TSV to table. | markdowntable.tsvToTable | Shift + Alt + T | Yes (only when selecting range) | +| Navigate to next cell. | markdowntable.nextCell | Tab | No | +| Navigate to previous cell. | markdowntable.prevCell | Shift + Tab | No | +| Navigate to next cell (w/o format). | markdowntable.nextCellWithoutFormat | | No | +| Navigate to previous cell (w/o format). | markdowntable.prevCellWithoutFormat | | No | +| Insert column in the right. | markdowntable.insertRight | | Yes (only when not selecting range) | +| Insert column in the left. | markdowntable.insertLeft | | Yes (only when not selecting range) | +| Align to Left. | markdowntable.alignLeft | | Yes | +| Align to Center. | markdowntable.alignCenter | | Yes | +| Align to Right. | markdowntable.alignRight | | Yes | --- ## 2. Demo -### 2.1. Navigate to next cell (with auto insert row) +### 2.1. Navigate to next cell (with auto format & auto insert row) Key binding to `Tab`. @@ -30,6 +32,11 @@ Key binding to `Tab`. - with auto format - ![navigate](images/navigate_next_cell.gif) +#### 2.1.1 Without auto format + +- Use markdowntable.nextCellWithoutFormat command +- If you want, you need to assign this command to some key binding by yourself. + ### 2.2. Navigate to prev cell Key binding to `Shift`+`Tab`. @@ -39,6 +46,11 @@ Key binding to `Shift`+`Tab`. - with auto format - ![navigate_prev](images/navigate_prev_cell.gif) +#### 2.2.1 Without auto format + +- Use markdowntable.prevCellWithoutFormat command +- If you want, you need to assign this command to some key binding by yourself. + ### 2.3. Convert to table from TSV text Key binding to `Shift + Alt + T`. @@ -83,7 +95,7 @@ What's focused on. ## 5. Release Notes -### - 0.3.0 (2020/10/27) +### 5.1. - 0.3.0 (2020/10/27) - [Add] Align column/columns commands diff --git a/images/insert.gif b/images/insert.gif index 5e4e90e..8a044a6 100644 Binary files a/images/insert.gif and b/images/insert.gif differ diff --git a/package.json b/package.json index 3fd2bf5..77f606d 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "publisher": "TakumiI", "displayName": "Markdown Table", "description": "A minimal extension for markdown table. Add features to edit markdown table.", - "version": "0.3.1", + "version": "0.4.0", "repository": { "type": "git", "url": "https://github.com/takumisoft68/vscode-markdown-table" @@ -21,6 +21,8 @@ "onCommand:markdowntable.insertLeft", "onCommand:markdowntable.nextCell", "onCommand:markdowntable.prevCell", + "onCommand:markdowntable.nextCellWithoutFormat", + "onCommand:markdowntable.prevCellWithoutFormat", "onCommand:markdowntable.alignLeft", "onCommand:markdowntable.alignCenter", "onCommand:markdowntable.alignRight" @@ -52,6 +54,14 @@ "command": "markdowntable.prevCell", "title": "Markdown Table: Navigate to previous cell." }, + { + "command": "markdowntable.nextCellWithoutFormat", + "title": "Markdown Table: Navigate to next cell (without auto format)." + }, + { + "command": "markdowntable.prevCellWithoutFormat", + "title": "Markdown Table: Navigate to previous cell (without auto format)." + }, { "command": "markdowntable.alignLeft", "title": "Markdown Table: Align to Left." diff --git a/src/extension.ts b/src/extension.ts index 4fe43d1..30acedf 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,7 +20,7 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(command); } - registerCommandNice('markdowntable.nextCell', (args) => { + let navigateNextCell = (withFormat :boolean) => { // エディタ取得 const editor = vscode.window.activeTextEditor as vscode.TextEditor; // ドキュメント取得 @@ -28,11 +28,12 @@ export function activate(context: vscode.ExtensionContext) { // 選択範囲取得 const cur_selection = editor.selection; // カーソル行 - const currentLine = doc.getText(new vscode.Selection( + const currentLine = new vscode.Selection( new vscode.Position(cur_selection.active.line, 0), - new vscode.Position(cur_selection.active.line, 10000))); + new vscode.Position(cur_selection.active.line, 10000)); + const currentLineText = doc.getText(currentLine); // テーブル内ではなかったら終了 - if (!currentLine.trim().startsWith('|')) { + if (!currentLineText.trim().startsWith('|')) { // 通常のインデント vscode.commands.executeCommand('tab'); return; @@ -66,8 +67,7 @@ export function activate(context: vscode.ExtensionContext) { const table_selection = new vscode.Selection( new vscode.Position(startLine, 0), new vscode.Position(endLine, 10000)); - const table_text = doc.getText(table_selection); - + let table_text = doc.getText(table_selection); // テーブルの変形処理クラス @@ -92,24 +92,40 @@ export function activate(context: vscode.ExtensionContext) { (isNextRow && prevRow >= tableData.cells.length + 1) ); - // 次の行が必要なら追加する - if (isInsertNewRow === true) { - tableData = mdt.insertRow(tableData, tableData.cells.length); + if (withFormat) { + // 次の行が必要なら追加する + if (isInsertNewRow === true) { + tableData = mdt.insertRow(tableData, tableData.cells.length); + } + + // テーブルをフォーマット + table_text = mdt.tableDataToFormatTableStr(tableData); } + else { + // 次の行が必要なら追加する + if (isInsertNewRow === true) { + table_text += '\n' + tableData.indent + '|' + ' |'.repeat(tableData.columns.length); + } - // テーブルをフォーマット - const newTableText = mdt.tableDataToFormatTableStr(tableData); + // | が足りていないときは追加する + if (currentLineText.split('|').length < tableData.columns.length + 2) { + let table_text_lines = table_text.split(/\r\n|\n|\r/); + const cursorRow = cur_selection.active.line - startLine; + table_text_lines[cursorRow] += '|'; + table_text = table_text_lines.join('\r\n'); + } + } //エディタ選択範囲にテキストを反映 editor.edit(edit => { - edit.replace(table_selection, newTableText); + edit.replace(table_selection, table_text); }); // 新しいカーソル位置を計算 // character の +1 は表セル内の|とデータの間の半角スペース分 const newColumn = (isNextRow === true) ? 0 : prevColumn + 1; const newRow = (isNextRow === true) ? prevRow + 1 : prevRow; - const [newline, newcharacter] = mdt.getPositionOfCell(newTableText, newRow, newColumn); + const [newline, newcharacter] = mdt.getPositionOfCell(table_text, newRow, newColumn); const newPosition = new vscode.Position( table_selection.start.line + newline, table_selection.start.character + newcharacter + 1); @@ -117,9 +133,9 @@ export function activate(context: vscode.ExtensionContext) { // カーソル位置を移動 editor.selection = newSelection; - }); + }; - registerCommandNice('markdowntable.prevCell', (args) => { + let navigatePrevCell = (withFormat :boolean) => { // エディタ取得 const editor = vscode.window.activeTextEditor as vscode.TextEditor; // ドキュメント取得 @@ -165,7 +181,7 @@ export function activate(context: vscode.ExtensionContext) { const table_selection = new vscode.Selection( new vscode.Position(startLine, 0), new vscode.Position(endLine, 10000)); - const table_text = doc.getText(table_selection); + let table_text = doc.getText(table_selection); @@ -187,26 +203,50 @@ export function activate(context: vscode.ExtensionContext) { return; } - // テーブルをフォーマット - const newTableText = mdt.tableDataToFormatTableStr(tableData); - - //エディタ選択範囲にテキストを反映 - editor.edit(edit => { - edit.replace(table_selection, newTableText); - }); + if (withFormat) { + // テーブルをフォーマット + table_text = mdt.tableDataToFormatTableStr(tableData); + //エディタ選択範囲にテキストを反映 + editor.edit(edit => { + edit.replace(table_selection, table_text); + }); + } // 新しいカーソル位置を計算 // character の +1 は表セル内の|とデータの間の半角スペース分 const newColumn = (prevColumn > 0) ? prevColumn - 1 : tableData.columns.length - 1; const newRow = (prevColumn > 0) ? prevRow : prevRow -1; - const [newline, newcharacter] = mdt.getPositionOfCell(newTableText, newRow, newColumn); - const newPosition = new vscode.Position( + const [newline, newcharacter] = mdt.getPositionOfCell(table_text, newRow, newColumn); + let newPosition = new vscode.Position( table_selection.start.line + newline, - table_selection.start.character + newcharacter + 1); + table_selection.start.character + newcharacter); + if( doc.getText(new vscode.Selection( + table_selection.start.line + newline, table_selection.start.character + newcharacter, + table_selection.start.line + newline, table_selection.start.character + newcharacter + 1)) === ' ') { + newPosition = new vscode.Position( + table_selection.start.line + newline, + table_selection.start.character + newcharacter + 1); + } const newSelection = new vscode.Selection(newPosition, newPosition); // カーソル位置を移動 editor.selection = newSelection; + }; + + registerCommandNice('markdowntable.nextCell', (args) => { + navigateNextCell(true); + }); + + registerCommandNice('markdowntable.prevCell', (args) => { + navigatePrevCell(true); + }); + + registerCommandNice('markdowntable.nextCellWithoutFormat', (args) => { + navigateNextCell(false); + }); + + registerCommandNice('markdowntable.prevCellWithoutFormat', (args) => { + navigatePrevCell(false); }); registerCommandNice('markdowntable.tsvToTable', () => {