Skip to content

Commit

Permalink
Merge pull request #20 from ilyakam/release/2.0.0
Browse files Browse the repository at this point in the history
chore(*): release version 2.0.0
  • Loading branch information
ilyakam authored Jun 4, 2018
2 parents 2043532 + ac424c9 commit 45cef96
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 63 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [2.0.0] - 2018-06-03
### Added:
- Ability to remove all duplicate lines within one or more selection(s)
- Ability to preserve selection after removal

### Changed:
- The `README` to reflect the new functionality including screenshots

### Removed:
- Ability to remove all lines that match the selection

## [1.2.1] - 2018-06-03
### Fixed:
- Ensure that lines of different lengths are not concatenated together
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# RemoveDuplicateLines - A Sublime Text Plugin

A plugin for [Sublime Text](http://www.sublimetext.com/) that allows you to remove duplicate lines from the file.
A plugin for [Sublime Text](http://www.sublimetext.com/) that allows you to remove duplicate lines from files and selections.

## Installation

* **Package Control**
1. [Install Package Control](https://packagecontrol.io/installation)
1. [Bring up the Command Palette](https://sublime-text.readthedocs.io/en/stable/reference/command_palette.html#how-to-use-the-command-palette) and type "Package Control: Install Package"
1. Type "RemoveDuplicateLines" and press <kbd>enter</kbd>

* **Directly**
1. Locate the `Packages` folder in the [data directory](http://docs.sublimetext.info/en/sublime-text-3/basic_concepts.html#the-data-directory)
1. Download the [latest version of RemoveDuplicateLines](https://github.com/ilyakam/RemoveDuplicateLines/releases/latest)
1. Extract the archive into the `Packages` folder

* **Development**
1. [Follow the instructions on the `CONTRIBUTING` guide](./CONTRIBUTING.md#getting-started)

Expand All @@ -26,16 +28,16 @@ There are two ways to use this plugin:
This removes all duplicate lines, leaving the first occurrence of each line to be unique to the entire file.

| Before | After |
| ------ | ----- |
| ![without_before](https://user-images.githubusercontent.com/183227/39089760-422a4866-4583-11e8-94e8-545983074fd4.png) | ![without_after](https://user-images.githubusercontent.com/183227/39089761-43da04f8-4583-11e8-9901-4a85e117d952.png) |
| :----- | :---- |
| ![without_selection_before](https://user-images.githubusercontent.com/183227/40892300-1451b4b8-674a-11e8-95e9-041738f3ecd1.png) | ![without_selection_after](https://user-images.githubusercontent.com/183227/40892301-146c9940-674a-11e8-99a8-1878699f5114.png) |

### With a selection

This removes all other duplicate selections from the file that match the initial selection.
This removes all duplicate lines within one or more selection(s).

| Before | After |
| ------ | ----- |
| ![with_before](https://user-images.githubusercontent.com/183227/39089762-483acb22-4583-11e8-8ac5-aa6bcb3bb01e.png) | ![with_after](https://user-images.githubusercontent.com/183227/39089763-4a46dbcc-4583-11e8-9c65-c48674dcf77c.png)
| :----- | :---- |
| ![with_selection_before](https://user-images.githubusercontent.com/183227/40892302-1488c674-674a-11e8-9dce-92fff33cb903.png) | ![with_selection_after](https://user-images.githubusercontent.com/183227/40892303-14b18f64-674a-11e8-9d81-cf865d06565b.png)

## Changelog

Expand Down
79 changes: 22 additions & 57 deletions remove_duplicate_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,36 @@
from collections import OrderedDict

class RemoveDuplicateLinesCommand(sublime_plugin.TextCommand):
def run(self, edit):
def dedupe(self, selection, edit):
"""
Removes duplicate lines so that each line contains a unique string
If a line is selected, it removes ONLY its duplicate occurrences elsewhere
in the file
Removes duplicate lines from the selected region by splitting it into lines,
adding them into an `OrderedDict` which automatically removes duplicates,
and combines everything back into a string with newlines
"""

def dedupe_file():
"""
Removes duplicates from the entire file by splitting it into lines, adding
them into an `OrderedDict` which preserves order and removes duplicates,
and combines everything back into a string with newlines
"""

lines = self.view.substr(sublime.Region(0, self.view.size())).splitlines()

text = '\n'.join(OrderedDict.fromkeys(lines))

replace(text)

def dedupe_selection(selection):
"""
Removes duplicates from the rest of the file where the selection exists on
other lines by keeping the lines that do not match the selection and
recombining them back into a string with newlines
"""

lines = []

for line in self.view.lines(sublime.Region(0, self.view.size())):
is_other_line = (self.view.substr(line) != self.view.substr(selection)
or (line.begin() == selection.begin()
and line.end() == selection.end()))
selection = self.view.expand_by_class(selection, sublime.CLASS_LINE_END)

if is_other_line:
lines.append(self.view.substr(line))
lines = self.view.substr(selection).splitlines()

text = '\n'.join(lines)
text = '\n'.join(OrderedDict.fromkeys(lines))

replace(text)
self.view.replace(edit, selection, text)

def main():
"""
Removes duplicates from non-empty selections only when they exist;
otherwise, removes duplicates from the entire file
"""

non_empty_selections = [selection for selection in self.view.sel()
if not selection.empty()]

if non_empty_selections:
[dedupe_selection(selection)
for selection in reversed(non_empty_selections)]

else:
dedupe_file()
def run(self, edit):
"""
Removes duplicate lines so that each line contains a unique string
If one or more multiline regions are selected, it removes the duplicate
lines that are confined within each of the regions
"""

def replace(text):
"""
Replaces the entire file with text and moves the cursor to the top
"""
non_empty_selections = [selection for selection in self.view.sel()
if not selection.empty()]

self.view.replace(edit, sublime.Region(0, self.view.size()), text)
self.view.sel().clear()
if non_empty_selections:
[self.dedupe(selection, edit)
for selection in reversed(non_empty_selections)]

self.view.sel().add(0)
else:
entire_file = sublime.Region(0, self.view.size())

main()
self.dedupe(entire_file, edit)

0 comments on commit 45cef96

Please sign in to comment.